【问题标题】:Codeigniter $this->db->affected_rows() always returns 1Codeigniter $this->db->affected_rows() 总是返回 1
【发布时间】:2011-12-22 15:59:40
【问题描述】:

我使用的是 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();

【问题讨论】:

  • 您介意展示您的代码吗?
  • 这取决于您在 $this->db->affected_rows 之前使用的查询,如果您可以显示您的代码,那么共享解决方案将很容易。
  • 嗯,这是我的错误。它现在工作正常

标签: codeigniter


【解决方案1】:

其实我的代码是这样的

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 个字符向下移动 1 行以解决此问题。您的问题似乎是作为 Off-topic: Typo 结束的完美候选。
猜你喜欢
  • 2012-05-14
  • 2015-07-30
  • 1970-01-01
  • 2012-02-23
  • 1970-01-01
  • 2016-01-30
  • 2012-07-23
  • 2017-04-10
  • 2014-10-22
相关资源
最近更新 更多