Click to See Complete Forum and Search --> : [SOLVED] mysql finding max value


emus
01-14-2006, 08:46 AM
Hi everyone,

I have the following code


$best=mysql_query("SELECT MAX(grade) FROM students") or die(mysql_error());

echo $best;



Why does "echo $best" not give me an actual value but a resource id?

The MySQL syntax I got at the following site http://www.webdevelopersnotes.com/tutorials/sql/online_mysql_course_finding_the_minimum_and_maximu m_values.php3

voidinit
01-14-2006, 02:31 PM
I'm don't know very much about PHP development, but my best guess going by the C and Connector/J APIs is that $best is not the actual result of the query. $best is a result set of the returned rows and must be parsed by something similar to what follows (presented in C):



while ((row = mysql_fetch_row(best) != null)){
printf("%s", row[0]);
}



Take a look at http://us2.php.net/mysql_query.

emus
01-14-2006, 04:01 PM
voidinit, thank you very much, that did the trick. I wasn't sure of what type $best would be.



$best=mysql_query("SELECT MAX(grade) FROM students") or die(mysql_error());

$gradeArr=mysql_fetch_array($best);

echo $gradeArr[0];


This gives me the desired result.

Thanks again,

emus