CodeIgniter activerecord, retrieve last insert id?


Question

Are there any options to get the last insert id of a new record in CodeIgniter?

$last_id = $this->db->insert('tablename',
    array('firstcolumn' => 'value',
    'secondcolumn' => 'value')
);

Considering the table consits of fields id (autoincrement) firstcolumn and secondcolumn.

This way you can use the insert id in the following code.

1
150
4/27/2014 9:41:54 AM

Accepted Answer

Shame on me...

I looked at the user guide and the first function is $this->db->insert_id();

This also works with activerecord inserts...

EDIT: I updated the link

274
9/25/2015 9:10:08 AM

Last insert id means you can get inserted auto increment id by using this method in active record,

$this->db->insert_id() 
// it can be return insert id it is 
// similar to the mysql_insert_id in core PHP

You can refer this link you can find some more stuff.

Information from executing a query


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon