0

我在codeigniter中有这样的查询

$update= $this->db->query("update aas set aa = 'aa'  where no=" . $this->db->escape($No) . ""); 

当我跑

echo  $this->db->affected_rows()  or echo $this->db->affected_rows($update) 

它返回-1

更新任何存在的行时,我得到 -1 即使我没有要更新的行,它仍然显示 -1。

问题是什么?我正在使用带有 mysqli 驱动程序的 codeigniter 2.1.0

我尝试在 phpmyadmin 中运行它,它根据数据为我提供了正确的 0 或 1 行。但是当我通过 codeigniter 运行它时,即使要更新的值已更改或保持不变,我也会得到 -1

查询始终在 codeigniter 中为真

是不是因为我打开了codeigniter mysqli驱动

4

2 回答 2

3

在执行“写入”类型查询(插入、更新等)时显示受影响的行数。

参考:这里

您的语句中不正确(引号)sql(因此未执行)

$update= $this->db->query("update aas set aa = 'aa'  where no=" . $this->db->escape($No) . ""); 

应该

$update = $this->db->query("update aas set aa = 'aa'  where no = '{$this->db->escape($No) }'"); 
echo  $this->db->affected_rows();
于 2012-05-05T14:26:22.660 回答
0

这完全取决于您,但您也可以将此查询转换为 CodeIgniter 的Active Record Class。它不仅会自动为您转义所有内容,而且更易于用于简单查询,并避免 SQL 语句中引号不正确的潜在问题。

因此,对于 Active Record,它看起来像这样:

$data = array(
    'aa' => 'aa'
);

$this->db->where('no', $No)->update('aas', $data);

echo $this->db->affected_rows(); // this should return 1 now
于 2012-05-05T22:50:36.407 回答