6

Suppose that in my system an user U1 create a new database entry in table A and I use lastInsertId function to get the ID (auto_increment field) of such entry, but almost at the same time (a little later) another user U2 do the same.

In this case, does lastInsertId return for U1 the ID of the entry added by the other user, since it is the most recent?

Thanks.

BrunoB
  • 337
  • 1
  • 4
  • 16
  • 1
    Answer in here valid for your question, http://stackoverflow.com/questions/10371306/best-way-to-find-the-last-inserted-id-in-mysql-using-php – Sahan H. Feb 02 '13 at 22:06
  • 1
    My understanding is that each user will be on a different connection so there's no problem. At the start of your script when you create a connection, you're essentially creating it for just that 1 run of your code. Another user will have a new connection unless you do some fancy stuff to share the connection. – mpen Feb 03 '13 at 00:10

1 Answers1

3

I believe you should only worry about race conditions in case your using a single connection for different threads. If your using a different connection for each request, you should be fine. Checkout http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html for more details.

Muc
  • 1,464
  • 2
  • 13
  • 31
  • You basically have to go out of your way to re-use your connection across your threads, so unless you did something like that, then it is ok. – Nelson Jul 15 '16 at 01:53