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'=>''];
   $this->db->insert('users', $input);
   $insertId = $this->db->insert_id();
   return  $insertId;

Related Posts

What is the Difference Between Web Application and Hybrid Mobile Application

Web App: Web App stored on a remote server and delivered over the internet through a browser. Web ap

10 Good ways to Promote a Business Website for Getting More Sales

1. Search Engine Optimization: Search Engine Optimization is the way toward expanding your site&#