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

EricP picture EricP · Jan 19, 2010 · Viewed 15.7k times · Source

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

Answer

EricP picture EricP · Jan 25, 2010
$lastId=$contactsTable->getAdapter()->lastInsertId();

This worked.