Codeigniter $ this-> db-> affected_rows()始终返回1

Nic*_*ick 10 codeigniter

我正在使用codeigniter版本2.0.3.我正在尝试使用更新查询后获取受影响的行数

$this->db->affected_rows
Run Code Online (Sandbox Code Playgroud)

即使没有更新行,它也总是返回1.我试过了

mysql_affected_rows()
Run Code Online (Sandbox Code Playgroud)

如果查询失败,则返回-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();
Run Code Online (Sandbox Code Playgroud)

Nic*_*ick 20

其实我的代码是这样的

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();
Run Code Online (Sandbox Code Playgroud)

我修改了它

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();
}
Run Code Online (Sandbox Code Playgroud)

它的工作正常.

非常感谢你的回复..