CodeIgniter, Tutorials

How to get last inserted id in CodeIgniter?

08 January 2019

Sometimes, we require to get last inserted id from database. so in this example we will learn that how to get last inserted id in codeigniter.

insert_id() is function of “db” library. db library provides several function for connecting database task. DB Class insert_id() method is used to get last insert id in codeIgniter. So here i give example method so you can understand how it is working.


function cc_add_post(){
   $input = array('name'=>'Test', 'description'=>'Test Description');

   $this->db->insert('items', $input);
   $insert_Id = $this->db->insert_id();

   return  $insert_id;

This code will return last inserted row id using insert_id() method.

Leave a Comments