Skip to content
Advertisement

PDO Mysql prepared statement last_insert_id returns wrong value on multi-insert?

I noticed that if I prepare a multi-insert statement and execute it into MySQL via PDO, and then request the last_insert_id, I get the first ID of the multiple inserted rows, not the last one. Specifically:

JavaScript

will create these rows on an empty table:

JavaScript

But the last_insert_id will return “1”. Is this a known issue or am I doing something wrong? Could someone verify/test/explain this? I am at a loss at what to do to get the proper last ID, save for doing an actual select which would be WAY slower.

Advertisement

Answer

It’s correct mysql behavior

http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement