I am trying to search my MySQL database based on a globalvariable but this is not functioning. Here is my code below. Ants hints will be appreciated.
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');
You have to update the 2nd line of the function PullOver
to:
$Valforthis = "select ".$GLOBALS['Whatis']." from graph where month = '$Monthselect' and year = '$dateY'";
When using global variables inside a function u need to use $GLOBALS
$Valforthis = "select global $Whatis from graph where month = '".$GLOBALS['Monthselect']."' and year = '$dateY'";
Sorry to link to w3fools but they have a pretty good example here. Generally speaking people will tell you this is bad practice though and that you're better off just passing the info you need into the function.
Another option is to pass the variable by reference
function PullOver(&$Monthselect){...}