2013-01-21 63 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

相關問題