|
-
May 29th, 2002, 07:00 PM
#1
Thread Starter
Stuck in the 80s
MySQL: ID of Last Record?
How can I retrieve the ID of the last record in my table?
-
May 29th, 2002, 07:26 PM
#2
Fanatic Member
mysql_result("select id from tableName",mysql_numrows("select id from tableName") -1,"id");
That should do it.
-
May 29th, 2002, 08:16 PM
#3
Thread Starter
Stuck in the 80s
Thanks, I'll give it a try.
-
May 29th, 2002, 09:35 PM
#4
Fanatic Member
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");
-
May 29th, 2002, 10:46 PM
#5
Thread Starter
Stuck in the 80s
This works for me:
PHP Code:
$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.
-
May 29th, 2002, 10:50 PM
#6
Fanatic Member
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
-
May 29th, 2002, 10:55 PM
#7
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.
-
May 29th, 2002, 11:01 PM
#8
Thread Starter
Stuck in the 80s
But weeks could go by before the script is run again. So LAST_INSERT_ID() wouldn't work.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|