我正在尝试基于globalvariable搜索我的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');
答案 0 :(得分:1)
您必须将函数PullOver
的第二行更新为:
$Valforthis = "select ".$GLOBALS['Whatis']." from graph where month = '$Monthselect' and year = '$dateY'";
答案 1 :(得分:0)
在函数中使用全局变量时,需要使用$ GLOBALS
$Valforthis = "select global $Whatis from graph where month = '".$GLOBALS['Monthselect']."' and year = '$dateY'";
很抱歉链接到w3fools,但他们有一个很好的例子here。一般来说,人们会告诉你这是不好的做法,你最好把你需要的信息传递给函数。
另一种选择是pass the variable by reference
function PullOver(&$Monthselect){...}