skip to Main Content

How To Get The Last Inserted Record Id On Codeigniter?

How to get the last inserted record Id on Codeigniter?


The Codeigniter is the Lightweight fast and object-oriented PHP Framework. Sometimes we need the last inserted record Id for inserting the related data into another table. When we are working on product module at that time we insert product data to the database and also we need to insert product-related data like product attribute insert to the database we need the product id for stored attribute data.

When we are working on product module at that time we insert product data to the database and also we need to insert product-related data like product attribute insert to the database we need the product id for stored attribute data.

We can get the last inserted record id using insert_id the function on Codeigniter So let’s see how we can get the last inserted record Id using DB function insert_id.

 $product = [
         'title'=>'the coding stuff', 
         'description'=>'the coding stuff', 
         'price' => '$10'
   ];

   $this->db->insert('product', $product);
   $productId = $this->db->insert_id();

   return  $productId;

As per above, the example we get the last inserted record Id using insert_id method od DB class on Codeigniter.

If you have any question after using above example write the comment below.

Saroj

I am a web designer and developer part-time blogger. Write something on some free time. Find my every day thought here :)

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top