Is there any codeigniter function to do this?
Well, lucky for you.... getting the last database inserted ID using CodeIgniter is very simple. All you need to do is call the $this->db->insert_id(); function after you have just finished inserting a new row into the database and this will return that last 'unique' ID.
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.
CodeIgniter 4 Query Builder insertID()Using the Query Builder Class, we can INSERT and retrieve the Last Insert ID value, leveraging the built-in $builder insertID() method, which is called on the $db connection instance. Again, visiting the URL, /cicrud/home/users, executes the users() Home Controller method.
If you are AUTO_INCREMENT with column, then you can use last_insert_id() method. This method gets the ID of the last inserted record in MySQL.
try this code:
$this->db->insert_id();
Refer: https://www.codeigniter.com/user_guide/database/helpers.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With