SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    4.2 -> 5.2 = Use of undefined constant errors?!

    I just copied a PHP based website to a new server. The old server PHP version was 4.2.2 the new server is 5.2.3. The scripts are running on the new server but they are generating a ton of PHP Notice: messages:

    PHP Notice: Use of undefined constant id - assumed 'id'

    These are triggered when I read a record from a mySQL table and then try to access the field values through an associative array like this:

    $query1 = "SELECT * FROM users";
    $result1 = mysql_query($query1);
    while ($row = mysql_fetch_array($result1))
    {
    echo "User ID = $row[id]<br>";
    }

    Is there a php.ini setting I need to change or do I need to change something in the PHP either to do with establishing the DB connection or running the query?

    Thanks

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The notices are showing up due to a configuration difference -- they were always being generated, but the error reporting level was low enough on the old server that they weren't printed to the output.

    Is that code snippet the one actually generating the notice? I don't think it should, while $row[id] outside of double quotes will. In that instance, id is only valid if it's a defined constant. When you want to index into an array by string, you need single or double quotes. Ex:

    PHP Code:
    echo "User ID = " $row['id'] . "<br />"
    PHP falls back to looking for a string by the same name when the constant isn't found, so the code will run correctly while generating a notice that this happened.

    You can also write that line as:
    PHP Code:
    echo "User ID = {$row['id']}<br>"
    ..without generating a notice. If you have separate development and production environments, and want to simply suppress the notices in the live site until you can fix them all, then you can turn down the error reporting level.

    http://us2.php.net/error_reporting
    http://us2.php.net/manual/en/ref.err...display-errors

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. Not that isn't the exact code. Here's an actual line that is generating the error.

    $location[$row[id]] = "$row[name]";

    It's generating the error regording the constant [id] not [name]

    There is quite a few lines of code to change. I assume this notice doesn't really hamper processing of the code. I just need to reaise the minimum error level so my error_log stops getting filled up. Or is it detrimental to run a site that is triggering these undefined constant errors all the time?

    It's been soooo long since I messed with the PHP config. Can't I override the default error level reporting at the place in the code that makes the DB connection or do I need to edit php.ini?

  4. #4
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    id should be 'id'

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    There's probably a configuration setting for what level of errors gets logged as well, which you may be able to override on a per-script basis with ini_set(). A notice is the least detrimental type of error -- essentially just a friendly warning that your syntax is incorrect but the code could be executed anyway. So there's no real issue running with notices; it'd just be nice to eliminate them in case they turn into actual errors in a future version of PHP that may be less forgiving.


Bookmarks

Posting Permissions

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