Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get autoincrement id after an insert query performed with a prepared statement

If I execute an insert query with a stored procedure with php and the mysqli_* functions, is there a way to retrieve the value of an autoincrement id field?
mysqli->insert_id does not seem to work.

like image 505
Davide Gualano Avatar asked Sep 19 '09 17:09

Davide Gualano


2 Answers

Are you sure the last query you preformed was an INSERT?

mysqli->insert_id seems the proper answer:

Return Values

The value of the AUTO_INCREMENT field that was updated by the previous query. Returns zero if there was no previous query on the connection or if the query did not update an AUTO_INCREMENT value.

like image 79
gnarf Avatar answered Sep 20 '22 02:09

gnarf


You could try to make a query to MySql like so:

SELECT LAST_INSERT_ID()

Not sure if it works with stored procedures though.

like image 30
PatrikAkerstrand Avatar answered Sep 19 '22 02:09

PatrikAkerstrand