3

How to retrieve last inserted user id? I have to use that user id for inserting that user id into next query that also should be done in this transaction only. my query:

$db->beginTransaction ();
$sql = $db->query ( "INSERT INTO user( user_id, title)
 VALUES ( :p_user_id, :p_title )",
 array ( 'p_user_id' => '', 'p_title' => $title ) );
RAS
  • 8,100
  • 16
  • 64
  • 86
vvr
  • 456
  • 3
  • 17
  • 30

3 Answers3

4

You can use this method to retrive the last insert id :

$db->lastInsertId() 

With you code :

$db->beginTransaction ();
$sql = $db->query ( "INSERT INTO user( user_id, title) VALUES ( :p_user_id, :p_title )",
array ( 'p_user_id' => '', 'p_title' => $title ) );
$db->lastInsertId() ;
David Buros
  • 120
  • 7
2

Use insert() method for inserting data. Also it returns ID.

$db = new Zend_Db_Table('user');
$lastInsertId = $db->insert(array('user_id' => '', 'title' => $title));
Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143
1

Try this this should work:

$groupID = Engine_Db_Table::getDefaultAdapter()->lastInsertId();
Vishnu Prasad
  • 729
  • 1
  • 11
  • 28