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();
}