Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Prepared Statement, how I return the id of the inserted row?

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?

like image 515
Renato Dinhani Avatar asked Jul 02 '11 11:07

Renato Dinhani


People also ask

How do I get the unique ID for the last inserted 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.

How do I get the last row inserted id in SQL?

The LAST_INSERT_ID() function returns the AUTO_INCREMENT id of the last row that has been inserted or updated in a table.

What does PreparedStatement execute Update return?

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.

Can we use PreparedStatement for select query?

To retrieve data from a table using a SELECT statement with parameter markers, you use the PreparedStatement. executeQuery method.


1 Answers

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; 
like image 124
Renato Dinhani Avatar answered Sep 20 '22 22:09

Renato Dinhani