How To Get Last Insert ID In PHP Codeigniter?

How to get last insert ID in PHP Codeigniter?

In this article, we are going to discuss how to get the last inserted ID in Codeigniter. I will let you know how to get the last inserted id from the table. Sometimes, You have to work on relational tables where you perform the insert query on a table and you need that automatically generated ID to insert into the second table to create a relation between both tables.

In PHP, you can use the mysqli_insert_id function to get the last inserted ID.

In Codeigniter, You can use $this->db->insert_id(); function to get the last inserted ID.

insert_id() is function of "db" library. There is a number of query helper methods in Codeigniter.

function saveUserInfo(){
   $input = ['name'=>'blazing coders', 'email'=>'info.blazingcoders@gmail.com'];
   $this->db->insert('users', $input);
   $insertId = $this->db->insert_id();
   return  $insertId;
}

Related Posts
Animation Development Company

Blazingcoders is an Animation development company in India. We provide animation services to various

How to build eCommerce website using Laravel?

Below the blog post, we are going to discover how we can create and manage CMS and eCommerce website

Offshore And Outsource jQuery Developers

jQuery is a lightweight cross-browser JavaScript library designed to simplify the client-side script

Web Portal for Alumni Association for a leading Management Institute

Blazingcoders provides Web portal development for Alumni Association. Where every one Get all alumni

Get free quote for

How to get the last insert ID in Codeigniter

How to get the last insert ID in PHP

Contact us