mysql_insert_id() a 'Race Condition' ?

Is it possible for mysql_insert_id() to undergo a 'Race Condition' ? It would be my best guess that this function has exclusive process control (even in a hyperthreaded situation) such that only one client can call this at once (first in, first out queue with no acynchornous or concurrent behavior).

Is this true?

LVL 11
level9wizardAsked:
Who is Participating?
 
hieloConnect With a Mentor Commented:
mysql_insert_id() will give you the last auto id  on the current connection. If there are multiple processes running which have a mysql connection open AND all of them insert into a table, thereby generating an auto id, when you call mysql_insert_id() on each of these processes, you will get the auto id generated on each of the respective connections. So, if this were to happen:

proccess1: inserted a record (resulting in id 3)
proccess2: inserted a record (resulting in id 4)
process1: mysql_insert_id() (will return 3 - the last auto id of process 1 - NOT the max id on the table)
proccess3: inserted a record (resulting in id 5)
proccess4: inserted a record (resulting in id 6)
process2: mysql_insert_id() (will return 4 - the last auto id of process 2 - NOT the max id on the table)
0
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
it's actually not like that that it works.
it's the mysql server holding the information, per session/table, of the last generated autonumber, and it returns that information to the function.

but the result for you is the same: every session will get it's own generated id values via the mysql_insert_id() function.
0
 
level9wizardAuthor Commented:
It sounds like your answers differ, I think I've made this more complicated in my first question than it needs to be.

Let me be more to the point with a real example:

On one page I run a query produced from some $_POST'ed form data that inserts a row, and stores the mysql_insert_id()  into a PHP $_SESSION;

On the next page, some more information from the user is requested and the row is UPDATE'ed using WHERE the id is equal to my $_SESSION id.

My fear is that if person A was a little bit slow in their steps thru the two form pages, and person B came in and signed up after them, but was quicker to page two, would person B in fact be using the mysql_insert_id() from person A (since I'm storing it in my own session)?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
as it will be your page that runs both the insert and the mysql_insert_id() on the same database session, that site browsing session will get IT's own mysql id. it will store that in it's own $_SESSION array, which again is dedicated to that user's site browsing session.
0
 
level9wizardAuthor Commented:
angelIII, so to the point - no, there's not chance Person B will get Person A's mysql_insert_id() ?
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
correct.
0
 
hieloCommented:
>>It sounds like your answers differ,
No:
>>>>you will get the auto id generated on each of the respective connections
>>>>but the result for you is the same: every session will get it's own generated id values via the mysql_insert_id() function.

>>there's not chance Person B will get Person A's mysql_insert_id()
No.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.