Jump to content

Archived

This topic is now archived and is closed to further replies.

cujo

Getting the ID of the most recent database entry

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?

Share this post


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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.