I want retrieve the id of a inserted row in the database, but I don't know how to do this.
I tried to return using the SQL clause RETURNING id
, but not works.
How I can return the id after the insertion of a row?
9 Obtaining the Unique ID for the Last Inserted Row. If you insert a record into a table that contains an AUTO_INCREMENT column, you can obtain the value stored into that column by calling the mysql_insert_id() function.
The LAST_INSERT_ID() function returns the AUTO_INCREMENT id of the last row that has been inserted or updated in a table.
When the method executeUpdate is used to execute a DDL (data definition language) statement, such as in creating a table, it returns the int value of 0.
To retrieve data from a table using a SELECT statement with parameter markers, you use the PreparedStatement. executeQuery method.
After calling the execute()
method on the Prepared Statement, the id of the insert row will be in the insert_id
attribute.
$pstm->execute(); $pstm->insert_id;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With