SQL中的PHP全局变量


PHP Global Variable in SQL

我正在尝试基于全局变量搜索MySQL数据库,但这不起作用。下面是我的代码。蚂蚁的提示将不胜感激。

if (!isset($Whatis)) {
    echo "Value for Whatis is not set";
} else {
     echo $Whatis ;
}
function PullOver($Monthselect) {
    $dateY = date("Y");
    $Valforthis = "select global $Whatis from graph where month = '$Monthselect' and year = '$dateY'";
    echo $Valforthis ;
    $retrieve = mysql_query($Valforthis);
    $Val = mysql_fetch_array($retrieve);
    echo $Val['$Whatis'] ;
}
PullOver('January');

您必须将函数PullOver的第2行更新为:

$Valforthis = "select ".$GLOBALS['Whatis']." from graph where month = '$Monthselect' and year = '$dateY'";

在函数内部使用全局变量时,需要使用$GLOBALS

$Valforthis = "select global $Whatis from graph where month = '".$GLOBALS['Monthselect']."' and year = '$dateY'";

很抱歉链接到w3傻瓜,但他们在这里有一个很好的例子。一般来说,人们会告诉你这是一种糟糕的做法,你最好只是把你需要的信息传递到功能中。

另一种选择是通过引用传递变量

function PullOver(&$Monthselect){...}