mysql_insert_id for a query?
I am trying to add a record to a mysql database and retrieve that records id immediately after. I am looking to use mysql_insert_id() however I am wondering does it pull the very last record 开发者_开发百科or the last record you have inserted?
What i'm thinking if two users of the website both run querys then the last record added may not be that users record, so does mysql_insert_id() differentiate between the two or just pull the last record?
Thanks.
very last record or the last record you have inserted?
a latter one. it is clearly stated on this function's manual page
You do not need to worry about 2 users colliding as mysql_insert_id() will return the auto_increment ID of the last record inserted on that connection resource. The other user should have a different MySQL connection.
So, if you have...
$r = mysql_query("INSERT INTO foo VALUES('bar')");
$id = mysql_insert_id()
You'll get the ID from that INSERT statement.
If you have multiple connections in the one script, just make sure you're passing the optional link identifier to your MySQL calls.
精彩评论