she*_*ado 5 php model codeigniter
我使用 Codeigniter 3.0.6 来构建一个系统。通常,我使用fluence_rows() 来检查我的数据库是否更新。
function update($id=0,$data=array())
{
$this->db->where($this->key, $id);
$this->db->update($this->table,$data);
if($this->db->affected_rows() > 0)return TRUE;
else return FALSE;
}
Run Code Online (Sandbox Code Playgroud)
问题是如果没有找到更新,它也会返回 0。因为我想在我的代码中区分更新和无更新,所以我使用这个解决方案。
function update($id=0,$data=array())
{
$this->db->trans_start();
$this->db->where($this->key, $id);
$this->db->update($this->table,$data);
$this->db->trans_complete();
if ($this->db->affected_rows() > 0) {
return TRUE;
} else {
if ($this->db->trans_status() == FALSE) {
return FALSE;
}
return 'No Update';
}
}
Run Code Online (Sandbox Code Playgroud)
但即使在更新后的affected_rows 总是返回int(0)。什么原因?谢谢你。
小智 0
我还使用 CI 3.0.6 和affected_rows() 返回正确数量的更新行。
您可以在没有交易的情况下进行测试吗?像这样的东西?
function update($id)
{
$this->db->set('field_name', "new_value");
$this->db->where('id', $id);
$this->db->update('table_name');
$rows = $this->db->affected_rows();
return $rows;
}
Run Code Online (Sandbox Code Playgroud)
验证数据库中的记录是否确实已更新。您可以检查 $rows 的更新状态。
if ($rows < 1) {
// no update
} else {
// updated
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1019 次 |
| 最近记录: |