How to get value from last inserted row using Codeigniter
Is there any codeigniter feature?
+3
Hasnain
source
to share
6 answers
try this code:
$this->db->insert_id();
See: http://ellislab.com/codeigniter/user_guide/database/helpers.html
+9
Mani
source
to share
There is no special function for this. But you can get the row id you just inserted and then get the data:
$this->db->insert('table_name', $data);
$id = $this->db->insert_id();
$q = $this->db->get_where('table_name', array('id' => $id));
return $q->row();
+5
Yura Loginov
source
to share
Use Active Record method after insert. return the last inserted id
function insert($data)
{
$this->db->insert('tablename', $data);
return $this->db->insert_id();
}
Here is a link
+2
Muhammad raheel
source
to share
I have some tips for you. When you want to insert data, return the dataset and you can edit it.
public function set_alb_photos() {
$data = array(
'alp_title' => $this->input->post('alp_title'),
'alp_file_location' => $this->input->post('alp_file_location'),
'alp_album_id' => $this->input->get('alb_id'),
'alp_created_at' => date("Y-m-d H:i:s"),
'alp_updated_at' => date("Y-m-d H:i:s")
);
$this->db->insert('alb_photos', $data);
return $data;
}
I hope this help.
+2
akbarbin
source
to share
What about:
$this->db->get('mytable', 1,0)
0
rax
source
to share
This will help you get the last inserted row from the table.
$last = $this->db->order_by('id',"desc")->limit(1)->get('tablename')->row();
print_r($last);
0
Waseem shah
source
to share