Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

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

    [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);
    mysql_close($d);
    while ($a = mysql_fetch_array($raw_data)){
    $key = $a['field'];
    $val[$key] = $a['value'];
    }
    return $val;
    }

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    do you close the connection to the database before you use the data you want to retrieve?

  • #3
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #5
    New Coder
    Join Date
    Jun 2007
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts
    so just add $val = array(); in the beginning?

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Yes. Any place inside of the function before it's used(in a loop or otherwise).

  • #7
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •