I'm inserting a new row into my database with this code:
$data = array(
'key' => 'value'
);
$this->getDbTable()->insert($data);
How can I get the row id of the this row that I just created?
If you are AUTO_INCREMENT with column, then you can use last_insert_id() method. This method gets the ID of the last inserted record in MySQL.
You can get the id of the last transaction by running lastInsertId() method on the connection object($conn).
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.
Did you try this ? This also works fine.
//just after you call your insert($data) function .. use this
$lastInsertId = $this->getAdapter()->lastInsertId();
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