View Full Version : Can someone help me with this error >> PHP Notice: Undefined variable

09-05-2007, 06:18 PM
[Wed Sep 05 12:09:28 2007] [error] PHP Notice: Undefined variable: val in /srv/www/includes/config.inc.php on line 94

Line 94 is bolded and underlined:

function get_data($uid){
$query = "SELECT * FROM data WHERE userid = '$uid'";
$d = db();
$raw_data = mysql_query($query, $d);
while ($a = mysql_fetch_array($raw_data)){
$key = $a['field'];
$val[$key] = $a['value'];
return $val;

09-05-2007, 06:28 PM
do you close the connection to the database before you use the data you want to retrieve?

09-05-2007, 06:54 PM
I think the scope on $val is only available to the while loop. You need to declare the $val array at the start of the function.

09-05-2007, 08:47 PM
I think the scope is OK, but if the query returns an empty result set then the stuff in the while loop will never run and the variable will never be created.

To be safe you should declare the variable as an empty array at the beginning of the function.

09-05-2007, 10:59 PM
so just add $val = array(); in the beginning?

09-05-2007, 11:15 PM
Yes. Any place inside of the function before it's used(in a loop or otherwise).

09-06-2007, 12:31 AM
I think the scope on $val is only available to the while loop.

PHP is unlike Java (and I believe C#) in that the scope is not limited to whatever the current braces are at the point of definition.