Skip to content
February 29, 2008 / ranacse05

Error With MAX() in MySQL


Today i was trying to get the maximum value of a column in mysql.In mysql “select max(column_name) from table_name ;” works fine.But i was facing problem to get the value by mysql_query() in php.My code was

$re = mysql_query(“Select max(id) from test”);
echo $re;

It shows wrong output.

I just change it to

$sql=”select max(id) from test;”;
$val=mysql_query($sql);
$row=mysql_fetch_assoc($val);
echo $row[‘max(id)’];

And its work fine 🙂

Advertisements

3 Comments

Leave a Comment
  1. mahfuz / Mar 4 2008 7:03 pm

    thats cute.

  2. pureform / Mar 6 2008 12:51 am

    The variable that mysql_query() returns is a special PHP type called ‘resource’ … once you have declared a resource [by calling mysql_query()], you can act upon it [like you did with mysql_fetch_assoc()].

    Another way is to do this:

    $r = mysql_query("SELECT MAX(`id`) AS `maxID` FROM `tblexample`");
    echo mysql_result($r,0,"maxID");

    See here: http://www.php.net/mysql_query

  3. ranacse05 / Mar 6 2008 6:44 am

    @pureform
    Thanks for ur idea.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: