2013-01-21 33 views

回答

9

试试这个代码:

$this->db->insert_id(); 

参见: http://ellislab.com/codeigniter/user_guide/database/helpers.html

+0

这是用来得到最后插入的id,但我想要得到该行。 – Hasnain

+1

在此ID的帮助下,您可以检索上次插入的行。 – Mani

+0

如果您刚刚插入该行,则已具备详细信息 - 为什么需要从数据库中取回数据? @Mani已经展示了如何获得插入ID。 – dakdad

0

什么:

$this->db->get('mytable', 1,0) 
+0

这将用于应用限制。参见[Here](http://ellislab.com/codeigniter/user-guide/database/active_record.html#select) –

2

插入后使用Active Record的方法。返回最后插入的ID

function insert($data) 
{ 
    $this->db->insert('tablename', $data); 
    return $this->db->insert_id(); 
} 

Here是参考

5

没有特殊的函数,该函数。但是,您可以获取刚插入的行的ID,然后获取它的数据:

$this->db->insert('table_name', $data); 

$id = $this->db->insert_id(); 
$q = $this->db->get_where('table_name', array('id' => $id)); 
return $q->row(); 
2

我有针对您的提示。当你想插入数据时,请返回你的数据数组,然后你可以编辑它。

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; 
    } 

我希望能有所帮助。

0

这将帮助您从表中获取最后插入的行。

$last = $this->db->order_by('id',"desc")->limit(1)->get('tablename')->row(); 
    print_r($last); 
+0

提供一些描述。 – Masoud

相关问题