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

Renato Dinhani picture Renato Dinhani · Jul 2, 2011 · Viewed 37.9k times · Source

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?

Answer

Renato Dinhani picture Renato Dinhani · Jul 7, 2011

After calling the execute() method on the PreparedStatement, the id of the insert row will be in the insert_id attribute. Only read it.

$pstm->execute();
$pstm->insert_id;