2011-12-22 126 views
6

我正在使用codeigniter版本2.0.3。我試圖用

$this->db->affected_rows 

它總是返回即使沒有行已更新1更新查詢後得到受影響的行數。我試着用

mysql_affected_rows() 

它返回-1爲查詢失敗和0如果沒有記錄已更新。

編輯包括我的代碼

我只是用

$country_id = $this->input->post('country_id'); 
$time=$this->input->post('time'); 

$insert_array = array(
    'country' => $this->input->post('name') 
); 
$this->db->update('country_master', $insert_array, array("country_id" => $country_id,"last_updated"=>$time)); 
$afftectedRows=$this->db->affected_rows(); 
+1

你介意顯示你的代碼嗎? – 2011-12-22 16:05:21

+0

這取決於您之前使用的查詢 $ this-> db-> affected_rows 如果您可以顯示您的代碼,將很容易共享該解決方案。 – 2011-12-22 16:48:31

+0

H'mmm這是我的錯誤,現在工作正常 – Nick 2011-12-23 09:13:57

回答

13

其實我的代碼是這樣

if (!$country_id) { 
    $this->db->insert('country_master', $insert_array); 
    $id = $this->db->insert_id(); 
} else { 
    $this->db->update('country_master', $insert_array, array("country_id" => $country_id, "last_updated" => $time)); 
} 
$afftectedRows = $this->db->affected_rows(); 

而且我修改了它

if (!$country_id) {     
    $this->db->insert('country_master', $insert_array); 
    $id = $this->db->insert_id(); 
} else { 
    $this->db->update('country_master', $insert_array, array("country_id" => $country_id, "last_updated" => $time)); 
    $afftectedRows = $this->db->affected_rows(); 
} 

而且現在工作很好。

非常感謝你的答覆..

+1

謝謝,@尼克。 任何技術來獲取多行插入時受影響的行? – 2017-06-18 10:22:06