To get Last inserted results from the MySQL table in Codeigniter use limit(). when you insert some values into the database and get this value at the same time, you need to use descending order to get the result. If you want to get results using descending order with the Limit so, try with this code:
<?php // -- Codeigniter Last id Select -- // $this->db->select('*'); $this->db->from('Table Name'); $this->db->order_by("id", "desc"); $this->db->limit(1); $query = $this->db->get();
Get last inserted id from database in Codeigniter use $this->db->insert_id();
If you want to get the last inserted id after insert success then follow this example:
<?php public function InsertData($data){ $ins_qry=$this->db->insert('table name',$data); if($ins_qry){ $last_id=$this->db->insert_id(); } } ?>
If you want to get another table value using the last inserted id then follow this example:
<?php public function InsertData($data){ $ins_qry=$this->db->insert('table name',$data); if($ins_qry){ $last_id=$this->db->insert_id(); $sec_qry = $this->db->select('*')->from('nest table name')->where('fk_some_id',$last_id)->get()->result(); } } ?>
See more Helpful Codeigniter Tutorial post. Visit fast.
Question and answer is powered by AnsPress.io