Skip to content
Advertisement

PHP/MySQL Get autoincremented value after insert

In my mysql table i have an id-column which is set to autoincrement.

then i do queries like this:

INSERT INTO table (id, foo) VALUES (”, ‘bar’)

how can i then safely find out which id was generated with this insert?

if i just query the last id this might not be safe, since another insert could have happened in the meantime, right?

Advertisement

Answer

There’s a PHP and also a MySQL function for this: mysqli_insert_id() and PDO::lastInsertId().

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

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