Persistent undefined index error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fluidbyte
Forum Commoner
Posts: 30
Joined: Tue May 27, 2008 2:07 pm

Persistent undefined index error

Post by fluidbyte »

I'm simply trying to retrieve a max value from my database, and no matter what I do I get an undefined index error for the "navPosition" (line 14) regardless of if the query returns any results or not. Any ideas what might be causing this?

Code: Select all

 
$sql_query = "SELECT MAX(navPosition) FROM " . $System["tblPrefix"] . "_navigation WHERE navParent=" . $navParent;
        
        $rs = mysql_query($sql_query);
        
        $row = mysql_fetch_array($rs);
        
        if (!isset($row))
          {
          $navPosition = 0;
          }
        else
          {       
          $navPosition = $row["navPosition"];
          }
 
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Persistent undefined index error

Post by onion2k »

You're not returning navPosition in your SQL, so it's not in the array. MAX(navPosition) is not the same as navPosition.

To fix it, use an alias.. eg MAX(navPosition) AS max_navPosition ... then use max_navPosition as the index in the PHP.
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: Persistent undefined index error

Post by nowaydown1 »

To expound on what onion said just a little, you need to provide an alias for your MAX(navPosition). So just switch your query to MAX(navPosition) as `navPosition` or something along those lines and it should work for you then.
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Re: Persistent undefined index error

Post by mchaggis »

Change your SQL to:

SELECT MAX(navPosition) AS navPosition FROM....

Alternatively try,

Code: Select all

 
$sql_query = "SELECT MAX(navPosition) FROM " . $System["tblPrefix"] . "_navigation WHERE navParent=" . $navParent;
        
$rs = mysql_query($sql_query);
        
$row = mysql_fetch_row($rs);
$navPosition = 0;
 
if (is_array($row)) {
        $navPosition = intval(@array_shift($row));
}
 
fluidbyte
Forum Commoner
Posts: 30
Joined: Tue May 27, 2008 2:07 pm

Re: Persistent undefined index error

Post by fluidbyte »

lol, thanks. One of those things that after I saw the answer I kicked myself.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Persistent undefined index error

Post by onion2k »

Just a personal preference really, but aliasing things to existing column names is a bad idea in my opinion. When your code grows to the point where several people are required to maintain it and the SQL is separated from the PHP (eg stored procedures) it'll stop being obvious what 'navPosition' is ... is it the value from the column? Is it the MAX() value? Is it something else?

It's better to use a logical name that tells the developer exactly what is being returned - max_navPosition is much less ambiguous.
Post Reply