Tuesday, August 19, 2008

How to get Max ID row from mysql table using php

Do you need to find the max id of a mysql table? check out the following code.
simply use the sql MAX function to find the max id.

<?php

mysql_connect("localhost","root","");
mysql_select_db(test);

$q = "select MAX(id) from tbl_name";
$result = mysql_query($q);
$data = mysql_fetch_array($result);

echo $data[0];

?>


21 comments:

  1. how to find max five value from table

    ReplyDelete
  2. you can try something like this,

    select col_name from tbl_name order by col_name desc limit 5

    ReplyDelete
  3. how to get max value from ms access database using php

    ReplyDelete
  4. thanks you for super simple query

    ReplyDelete
  5. This didnt work :/ It gave me the error

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in "not going to show PATH" on line 8

    ReplyDelete
  6. Thank u so much for this!! Ive been trying to get this work for hours and your was the only site which actually helped!

    ReplyDelete