Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework: How to retrieve the id of the last inserted row?

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?

like image 431
Andrew Avatar asked Dec 08 '09 18:12

Andrew


People also ask

How do you find the last inserted value?

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.

How can I get last insert ID in PDO?

You can get the id of the last transaction by running lastInsertId() method on the connection object($conn).

How do I get the last inserted id in MySQL?

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.


1 Answers

Did you try this ? This also works fine.

//just after you call your insert($data) function .. use this
$lastInsertId = $this->getAdapter()->lastInsertId();
like image 169
Amit Dugar Avatar answered Sep 18 '22 13:09

Amit Dugar