Thread: mysql_insert_id question
07-12-03, 06:52 #1Registered User
- Join Date
- Apr 2002
Unanswered: mysql_insert_id question
let's consider the example code of mysql_insert_id I take from php.net:
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Impossible de se connecter : " . mysql_error());
mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf ("Le dernier ID inséré dans est le %d\n", mysql_insert_id());
Assuming that there are two processes (A & B) that are concurrently executing the above script. WHat I am not clear is the race condition problem. When A finishes the line "mysql_query..", the OS switches to process B. B also executes until it finishes the line "mysql_query..". And at this time, the OS switches back to A. A goes on with "printf.." line. So when A calls mysql_insert_id , which id is returned : of the record inserted from A or from B ?
10-09-03, 13:44 #2Registered User
- Join Date
- Aug 2003
- Bologna - Italy
You analisys is wrong.
mysql_insert_id has a parameter, which can be omitted (the last opened connection handle would be used), which is the DB connection handle, which is returned by mysql_connect, that your code discards.
So, if another process would call the function, it will surely have a different db connection handle, so no confusion arises.The only failure is not trying to do it.