Question

In a high traffic application, is it possible for mysqli_insert_id() to return the wrong id, or to confuse the id between two INSERT queries performed nearly simultaneously?

Was it helpful?

Solution

No. mysqli_insert_id returns the most the AUTO_INCREMENT value from the most recent INSERT query on the current connection. It will never get confused with another connection, for example.

OTHER TIPS

No. How can I be so sure? Because it would have been reported and fixed a long time ago.

Quoting from a previous answer to this exact same question:

Look at http://dev.mysql.com/doc/refman/5.6/en/getting-unique-id.html for more information, it says this:

"For LAST_INSERT_ID(), the most recently generated ID is maintained in the server on a per-connection basis. It is not changed by another client. It is not even changed if you update another AUTO_INCREMENT column with a nonmagic value (that is, a value that is not NULL and not 0). Using LAST_INSERT_ID() and AUTO_INCREMENT columns simultaneously from multiple clients is perfectly valid. Each client will receive the last inserted ID for the last statement that client executed."

So you should be fine doing what you want and you shouldn't get strange results.

Yes it is posible! If you have multiple tables to insert with same connection and your last insert was incorrect (like I trying insert unescaped quotes in text).

I think it returns last successful insert id which can by from different table. I just spent hour to find this. Peace

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top