php - What do I need to do, if anything, to ensure $mdb2->lastInsertID() returns the auto-increment value for that connection? -


i've made switch sql server mysql, , can't find solid enough answer question anywhere:

i'm using php, mysql, , innodb table engine on tables need lock. standard series of 3 statements:

  1. insert table a
  2. $id = $mdb2->lastinsertid()
  3. insert table b (name, fk) values ('foo', $id)

what steps need take make sure $id has value of insert step 1? fine way is? need in transaction? need add other queries lock , release tables?

thanks guys.

you should/could, don't need anything: information-functions , function_last-insert-id

the id generated maintained in server on per-connection basis. means value returned function given client first auto_increment value generated recent statement affecting auto_increment column client. value cannot affected other clients, if generate auto_increment values of own. behavior ensures each client can retrieve own id without concern activity of other clients, , without need locks or transactions.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -