Get the Last Inserted ID in CodeIgniter

When you insert a new record into a database table, the last inserted ID is often required. In CodeIgniter, there is a method called $this->db->insert_id() which returns the last inserted ID.

This can be useful, for example, when adding new users to a database where you need to know the newly generated user ID.

To get the last inserted ID, you would use the following code:

$id = $this->db->insert_id();
//query info based on this $id here

This would return the last inserted ID as an integer.

$this->db->set('website', 'tleapps.com');
$this->db->set('type', 'blog');
$inserted = $this->db->insert("table_website");
if($inserted){
    $last_id = $this->db->insert_id();
}
Scroll to Top

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close