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.
views:
761answers:
4
+1
A:
You could try to make a query to MySql like so:
SELECT LAST_INSERT_ID()
Not sure if it works with stored procedures though.
PatrikAkerstrand
2009-09-19 17:32:33
+1
A:
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 anAUTO_INCREMENT
value.
gnarf
2009-09-19 17:42:01
You are right, I was making a mistake in my code, mysqli->insert_id actually works also with prepared statements. Thank you..
Davide Gualano
2009-09-20 12:13:52
A:
Maybe you can post your table definition.
Or double-check that there is an auto_increment
set.
Till
2009-09-19 18:03:30
+1
A:
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
dar7yl
2009-09-19 18:49:27