Get the new record primary key ID from MySQL insert query?

Let’s say I am doing a MySQL INSERT into one of my tables and the table has the column item_id which is set to autoincrement and primary key.

How do I get the query to output the value of the newly generated primary key item_id in the same query?

Currently I am running a second query to retrieve the id but this hardly seems like good practice considering this might produce the wrong result…

If this is not possible then what is the best practice to ensure I retrieve the correct id?

13 Answers
13

Leave a Comment