get the max value from mysql

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
jackie111
Forum Newbie
Posts: 13
Joined: Thu Jun 23, 2005 3:16 am

get the max value from mysql

Post by jackie111 »

here is the sql:
$resultc=mysql_db_query("webstat","select Max(cat) from info design");
how can i get the max(Cat)?
thanks
Jackie
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I'd alias the max(cat) if I were you to make it easier....

I have no idea how many records that is returning or what the data is so probably most generically....

Code: Select all

$resultc=mysql_db_query("webstat","select Max(cat) as m from info design");
$max_cat = array();
while ($row = mysql_fetch_array($resultc)) {
    $max_cat[] = $row['m'];
}
print_r($max_cat);
jackie111
Forum Newbie
Posts: 13
Joined: Thu Jun 23, 2005 3:16 am

Post by jackie111 »

the Max must will only return one line, i think we do not need to use while loop, can we use some function to get it?
thanks
Jackie
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

$resultc=mysql_db_query("webstat","select Max(cat) as m from info design");
$max_cat = mysql_result($resultc, 0, 'm');
EDIT | The PHP manual on http://www.php.net/ is the BEST resource for PHP available.... please use it ;)
jackie111
Forum Newbie
Posts: 13
Joined: Thu Jun 23, 2005 3:16 am

Post by jackie111 »

thanks so much!
Post Reply