Note the upgraded forum! If you are experiencing issues logging in, you may need to reset your password which should send an email. If the email doesn't arrive, be sure to check your spam folder just in case.

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