2011-12-22 129 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