Jump to content


Photo

Getting the ID of the most recent database entry


  • Please log in to reply
2 replies to this topic

#1 cujo

cujo
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 29 April 2006 - 07:16 PM

Hi, I'm new to these forums and I'd appreciate it if you could help with a problem I'm having.

I have a table where there is an auto_increment value that serves as the ID for each entry into the table. In my script I insert a new entry into the table, and I need the value of the ID for the entry that was just inserted.

Right now this is what I'm doing:
$id_query = mysql_query("SELECT id FROM table ORDER BY id DESC LIMIT 1");
$id = mysql_result($id_query,0);

That should get the ID for most recent entry from the table, right? There's one problem. Multiple entries are inserted into the table during the execution of the script, and I'm worrying that if more than one person is using the script at the same time that the values might get mixed up. Can anyone shed some light onto this problem?

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 29 April 2006 - 07:23 PM

[a href=\"http://ca.php.net/manual/en/function.mysql-insert-id.php\" target=\"_blank\"]php.net/manual/mysql-insert-id[/a] - does that help?
Legend has it that reading the manual never killed anyone.
My site

#3 cujo

cujo
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 29 April 2006 - 07:29 PM

[!--quoteo(post=369941:date=Apr 29 2006, 03:23 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Apr 29 2006, 03:23 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
[a href=\"http://ca.php.net/manual/en/function.mysql-insert-id.php\" target=\"_blank\"]php.net/manual/mysql-insert-id[/a] - does that help?
[/quote]
Awesome, thanks a lot man.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users