@lily
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' => '[email protected]', '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 insert_id()
method.
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.
@lily
In CodeIgniter, you can get the inserted ID after an insert operation by using the $this->db->insert_id()
method.
Here's an example of how you can do it in CodeIgniter:
1 2 3 4 5 6 7 8 9 10 |
$data = array( 'name' => 'John Doe', 'email' => '[email protected]' ); $this->db->insert('users', $data); // Insert data into the 'users' table $inserted_id = $this->db->insert_id(); // Get the inserted ID echo $inserted_id; // Display the inserted ID |
In the above example, we insert the data into the 'users' table using the $this->db->insert()
method. After the insert operation, we use $this->db->insert_id()
to get the ID of the inserted data. Finally, we can display the inserted ID using echo $inserted_id
.