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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Guy Hengel [angelIII / a3]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
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.