Forum Moderators: open

Message Too Old, No Replies

LAST INSERT ID() Concerns

Is it buggy?

         

JohnPorier

3:25 pm on Oct 24, 2007 (gmt 0)

10+ Year Member



I'm using LAST_INSERT_ID() to take auto-increment ID in a table and insert it in another table.

EX: An event table with a unique auto-increment ID and event details.
And a participants table where I want the same ID to be inserted so I can store event participants.

My question is whether or not this is a good way to go about this, and can LAST_INSERT_ID() get mixed up (will it ever pick up the wrong ID?) if the script that executes this PHP / MySQL is getting heavy traffic and being executed multiple times, at the same time.

This is just a concern I have, anyone know the answer?

Thanks,

coopster

3:39 pm on Oct 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The internal MySQL SQL function LAST_INSERT_ID() [dev.mysql.com] states ...


The last ID that was generated is maintained in the server on a per-connection basis. This means the value the function returns to a given client is the most recent AUTO_INCREMENT value generated by that client. The value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that you can retrieve your own ID without concern for the activity of other clients, and without the need for locks or transactions.

JohnPorier

3:42 pm on Oct 24, 2007 (gmt 0)

10+ Year Member



Woohoo, thanks. First time seeing that specific definition.