Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zend framework get last insert id of multi row insert using execute

How would I get the last inserted ID using a multirow insert? Here is my code:

$sql='INSERT INTO t (col1, col2, col3) VALUES (1, 2, 3), (4, 5, 6), (7, 8, 9)'; // example
$stmt = $contactsTable->getAdapter()->prepare($sql);
$stmt->execute(); 
$rowsAdded=$stmt->rowCount(); // mysql_affected_rows
$lastId=$stmt->lastInsertId();
echo '<br>Last ID: '.$lastId;

Also, is there a method in ZF to get the next insert id before an insert?

thanks

like image 246
EricP Avatar asked Jan 19 '10 14:01

EricP


People also ask

What does last insert ID return?

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


1 Answers

$lastId=$contactsTable->getAdapter()->lastInsertId();

This worked.

like image 96
EricP Avatar answered Sep 21 '22 16:09

EricP