Zend Database Get Last Insert ID
If you use the Zend Database class to normalize your database connections, this is how you get the id of the last inserted record. Just as in PHP you would call the mysql_insert_id() function to get the id of the last inserted record of a MySQL database connection.
$lastInsertId = $this->getAdapter()->lastInsertId();
and a more complete example, would look something like this, where you connect to a database, insert a record to the users table, and then get the id of the user that was inserted.
require_once('Zend/Db/Adapter/Pdo/Mysql.php'); $config = array('host' => 'localhost', 'username' => 'user', 'password' => 'secret', 'dbname' => 'db2' ); $zend_db = new Zend_Db_Adapter_Pdo_Mysql($config); $zend_db->insert('users',$user); $user_id = $zend_db->lastInsertId();
About this entry
You’re currently reading “Zend Database Get Last Insert ID,” an entry on PHP Functionalism
- Published:
- 12.7.11 / 2pm
- Category:
- Tips & Tricks, Zend
- Tags:
No comments
Jump to comment form | comments rss [?] | trackback uri [?]