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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s