2

We know that mysql_insert_id() will give the last inserted ID, but I am wondering if we could get a problem given the situation below.


UserA -> call a function that will insert a ROW to a TABLE and get the inserted ID

datetime of execution: 2013-10-01 10:18:25

ROW ID: 1


UserB -> call a function that will insert a ROW to a TABLE and get the inserted ID

datetime of execution: 2013-10-01 10:18:26

ROW ID: 2


Please notice that the time of execution is differ by only a second.

Assume that UserB finished inserted the row before UserA executed mysql_insert_id()

Does UserA will get the value of ROW ID: 1 or ROW ID: 2

Sam San
  • 6,567
  • 8
  • 33
  • 51

3 Answers3

5

No. mysql_insert_id() is also connection-safe, so for each user it will contain correct data since they will use different connections.

Please, note, that using mysql_ is bad idea since they are deprecated

Alma Do
  • 37,009
  • 9
  • 76
  • 105
3

Two users will have a different resource link to MySQL. PHP sort of does this for you by having the $link_identifier parameter as optional, but you should specify it.

See: http://php.net/manual/en/function.mysql-insert-id.php

Obligatory: don't use mysql_* functions, they are deprecated.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
0

The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.