Jump to content

Getting the ID of the most recent database entry


cujo

Recommended Posts

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?
Link to comment
Share on other sites

[!--quoteo(post=369941:date=Apr 29 2006, 03:23 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Apr 29 2006, 03:23 PM) [snapback]369941[/snapback][/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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.