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 5:47PM
    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 5:47PM
    Great! Thanks. save() looks like the better option.
Sign In or Register to comment.