Get last insert id?

edited October 2011 in Modules
I am using the insert method:

$this->examples_model->insert('values')

on an table with id as the autoincrement primary key.

How can I retrieve the new id that was inserted? Equivalent to PHP's mysql_insert_id() function.

Thanks in advance.

Comments

  • edited 11:34AM
    CodeIgniter has a $this->db->insert_id() method. In your case you could do the following:
    $this->examples_model->db()->insert_id();
    Alternatively, if you use MY_Model's "save" method instead, it will automatically return the insert_id:
    $id = $this->examples_model->save($values);
  • edited 11:34AM
    Great! Thanks. save() looks like the better option.
Sign In or Register to comment.