Click to See Complete Forum and Search --> : MySQL: ID of Last Record?
The Hobo
May 29th, 2002, 07:00 PM
How can I retrieve the ID of the last record in my table?
cpradio
May 29th, 2002, 07:26 PM
mysql_result("select id from tableName",mysql_numrows("select id from tableName") -1,"id");
That should do it.
The Hobo
May 29th, 2002, 08:16 PM
Thanks, I'll give it a try.
cpradio
May 29th, 2002, 09:35 PM
That will only work if you have id as the primary.
Also you may want to attempt:
$query = mysql_query("select id from tablename order by id desc");
mysql_result ($query,0,"id");
The Hobo
May 29th, 2002, 10:46 PM
This works for me:
$q = "SELECT id FROM newsitems ORDER by id ASC LIMIT 1, 1";
$id = mysql_fetch_array(mysql_query($q));
echo $id[0];
Thanks for your help. :)
cpradio
May 29th, 2002, 10:50 PM
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
scoutt
May 29th, 2002, 10:55 PM
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.
The Hobo
May 29th, 2002, 11:01 PM
But weeks could go by before the script is run again. So LAST_INSERT_ID() wouldn't work.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.