Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

SELECT LAST_INSERT_ID()

Not sure if it works with stored procedures though.

You could add this statement in your stored procedure after the insert:

   SET @saved_id = LAST_INSERT_ID()

Then, execute this query after calling the procedure:

   SELECT @saved_id

mysqli->insert_id (where mysqli represents your database connection) must be used directly after the insert. If you run other queries on the same connection before attempting to read insert_id you get 0 returned.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top