Skip to content
Advertisement

Query for maximal number in an INT column works well in phpmyadmin, but not in php code

I am trying to grab the MAX number of an INT column. When I run the query in phpmyadmin, it works fine. But when I put it in my PHP code, it keeps returning 0. I think my problem is I dont know how to grab it from the query properly. Specifically im using mysql_result, but after reading its syntax, I dont know if you can do it this way.

Here is what I got:

$query="SELECT MAX(`imageOrder`) FROM images where `gallery` = '$originalGallery'";
$result=mysql_query($query);    
$num=mysql_numrows($result);

I know the above works, but this is where I get lost. How do I get MAX(imageOrder) into a PHP variable?

$topOrder=mysql_result($result,0);

Doesn’t work:

$topOrder=mysql_result($result,$i,MAX(`imageOrder`));

Doesn’t work either (looping here BTW).

I am sure you know what I am trying to do. I am trying to grab a single returned value. It should just be a 2. Any ideas?

Advertisement

Answer

Try this :

$row = mysql_fetch_row($result);

echo $row[0]; // value

The row[0] will contain your maximun.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement