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 B开发者_运维知识库TW).
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?
You're almost there:
$query = "SELECT MAX(`imageOrder`) as maximages FROM images where `gallery` = '$originalGallery'";
$result = mysql_query($query);
$num = mysql_fetch_assoc($result);
echo $num['maximages'];
Try this :
$row = mysql_fetch_row($result);
echo $row[0]; // value
The row[0] will contain your maximun.
$query = "SELECT MAX(...) AS maximum ...";
$result = mysql_query($query) or die($query);
$row = mysql_fetch_assoc($result);
echo $row['maximum'];
Or, if you wan to fetch directly into a variable:
list($maximum) = mysql_fetch_row($result);
I could be completely wrong, but off the top of my head you need to fetch result as if it were a row using mysql_fetch_assoc()
or something similar.
精彩评论