Is mysql_insert_id safe to use?

According to the PHP documentation, it mysql_insert_idtakes the last inserted id from the mysql table.

My question is: if I have a site that inserts more than 2 rows per second into the database, can I use mysql_insert_idand get the correct identifier that I refer to to INSERTrequest a row earlier?

+5
source share
2 answers

In the MySQL manual:

The identifier that was generated is maintained on the server for each connection. This means that the value returned by the function for this client is the first AUTO_INCREMENT value generated for the most recent statement that affects the AUTO_INCREMENT column of that client. This value cannot affect other clients, even if they generate their own AUTO_INCREMENT values. This behavior ensures that each client can get its own identifier, without worrying about the activities of other clients, and without the need for locks or transactions.

Short version: safe to use.

+13
source

mysql_insert_id gives you the insert identifier of the last INSERT statement in the connection you give it.

, mysql, , , , .

+3

All Articles