简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM