In Codeigniter, you can use the
$this->db->insert_id() method to get the ID of the last inserted row. This method will return the ID of the row that was inserted last by the current connection.
Here's an example of how you can use this method to get the ID of the last inserted row:
1 2 3 4 5 6 7 8 9 10 11
$data = array( 'name' => 'John', 'email' => 'firstname.lastname@example.org', 'age' => 30 ); $this->db->insert('users', $data); $insert_id = $this->db->insert_id(); echo 'The ID of the last inserted row is: '.$insert_id;
This example inserts a new row into the
users table with the data provided in the
$data array, and then retrieves the ID of the inserted row using the
Note that this method only works if you are using an auto-incrementing primary key in your table. If you are not using an auto-incrementing primary key, this method will not work and you will need to use a different method to retrieve the ID of the inserted row.