Codeigniter:How to get the last inserted id?

In this small articale I will explain you how to get the last inserted ID from the database in codeigniter  .This is a very easy task.

You just need to write a single code line just after inserting the data and you will get the last inserted Id

from the database.

$data = array('name'=>'rakesh','lastname'=>'negi');
 $this->db->insert('tablename',$data);
 $last_id = $this->db->insert_id;//this will give you last inserted ID
 echo $last_id;


Hope this article will help you.Keep reading.