How to Get Last Inserted ID in Codeigniter?
Sometimes, we require to get last insert record id from database in controller, so in this example we will learn how to get last inserted row id in Codeigniter 3 project.
It's very often need to get last insert id in programming field, if you are working on Codeigniter framework and you want to fetch last created id, i mean max id, then you do easily.Codeigniter provide method insert_id() to get last inserted id.
insert_id() is function of "db" library. db library provides several function for connecting database task. insert_id() will return id of last inserted record. So here i give controller method so you can understand how it is working.
Controller Method
function add_item(){
$input = ['name'=>'Test', 'description'=>'This is Test'];
$this->db->insert('items', $input);
$insertId = $this->db->insert_id();
return $insertId;
}
As above example you can get simply last inserted row id using "insert_id()", So let's use this way.
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Codeigniter Ajax Pagination using JQuery Example
- Codeigniter Delete Multiple Rows using Checkbox Example
- How to Get and Set Config Variables in Codeigniter?
- How to Get Current Controller or Method Name in Codeigniter?
- Codeigniter Dynamic Dependent Dropdown using Ajax Example
- How to Get Last Record in Codeigniter?
- Codeigniter Generate PDF from View using Dompdf Example
- Codeigniter Ajax CRUD Tutorial Example
- Codeigniter Select2 Ajax Autocomplete from Database Example
- How to Get Current URL with Query String in Codeigniter?
- How to Get Current URL in Codeigniter?
- How to Get Last Inserted Id in Laravel?
- How to Get Query Log in Laravel Eloquent?