Codeigniter $this->db->affected_rows() 总是返回 1

标签 codeigniter

我正在使用 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();

最佳答案

其实我的代码是这样的

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

它现在工作正常。

并且非常感谢您的回复。。

关于Codeigniter $this->db->affected_rows() 总是返回 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8606437/

相关文章:

php - 模型在 MVC 编程中应该走多远?

php - 调用 javascript 函数内的 Controller

php - 将 REST API SDK Php for PayPal 与 Code Igniter 项目集成

php - Controller 内查询的 Codeigniter 更新

PHP 错误 : The Encrypt library requires the Mcrypt extension in codeigniter

database - 使用 codeigniter 编辑现有 pdf 文件并通过电子邮件发送到此编辑 pdf 文件

php - Codeigniter - 由于数据不起作用而返回 View

php - codeigniter phpexcel 错误 ZipArchive::getFromName():无效或未初始化的 Zip 对象

php - 即使在 codeigniter 中 db 的主机名错误,也继续执行

php - Bcrypt 在 codeigniter 中检查密码