how to get last inserted id in codeigniter example
In this article, we will explain to you how to get the last inserted id in Codeigniter. we will give a simple example of how to retrieve the last insert id into Codeigniter.
When we had fetched the last inserted ID into PHP, we were used to last auto-increment id. but CodeIgniter has provided an insert query (active record style) used to fetch the last insert id.
In this example, we will take a simple example of how to fetch the last inserted id from the table in CodeIgniter. Generally, the insert_id() CodeIgniter function returns the last inserting id from the Mysql database. Sometimes, when you need to insert a record of one table to another table at that time use the insert_id function. We hope you can learn something from this post.
Read Also: codeigniter last query print example
1 2 3 4 5 6 7 8 9 | public function add() { $arrData["first_name"] = $_POST['txtFirstName']; $arrData["last_name"] = $_POST['txtLastName']; $arrData["phone"] = $_POST['txtPhone']; $this->db->insert('users', $arrData); $insertId = $this->db->insert_id(); } |
Please follow and like us: