How can I retrieve the ID of the last record in my table?
Printable View
How can I retrieve the ID of the last record in my table?
mysql_result("select id from tableName",mysql_numrows("select id from tableName") -1,"id");
That should do it.
Thanks, I'll give it a try.
That will only work if you have id as the primary.
Also you may want to attempt:
PHP Code:$query = mysql_query("select id from tablename order by id desc");
mysql_result ($query,0,"id");
This works for me:
Thanks for your help. :)PHP Code:$q = "SELECT id FROM newsitems ORDER by id ASC LIMIT 1, 1";
$id = mysql_fetch_array(mysql_query($q));
echo $id[0];
btw, very nice site. It's definitely one for my bookmark's, you do have a typo though: "prever" should be "prefer" i believe. ;)
-Matt
why don't use just use
LAST_INSERT_ID()
always contains the most recently generated AUTO_INCREMENT value, and is not reset between queries
or
mysql_insert_id()
Returns the ID generated for an AUTO_INCREMENT column by the previous query. Use this function after you have performed an INSERT query into a table that contains an AUTO_INCREMENT field.
But weeks could go by before the script is run again. So LAST_INSERT_ID() wouldn't work.