gpt4 book ai didi

php - Codeigniter - 更新查询执行成功,但 $this->db->affected_rows() 返回 false

转载 作者:行者123 更新时间:2023-11-29 10:44:24 26 4
gpt4 key购买 nike

我正在开发 Codeigniter基于网络项目,我需要从管理面板更新用户。如果更新的值与数据库中已存在的值不同,则它可以正常工作。问题是$this->db->affected_rows()返回TRUE如果值不同则返回FALSE。

这是我的代码:-

// $where contains the array for where clause
// $data contains the array of user's data which includes the updated value

$this->db
->where($where)
->update('users', $data);

if($this->db->affected_rows() == true)
{
$response = array(
'message' => "User edited successfully",
'status' => true
);
}
else
{
// this else block always get executed if the updated value is equal to the value already exists in database. But in reality the record was updated successfully

$response = array(
'message' => "Unable to edit user",
'status' => false
);
}

return $response;

最佳答案

您可以使用transaction检查查询是否执行成功。之后,您可以检查 $this->db->affected_rows() 以查看管理员是否真的更新了用户值,并相应地返回消息。

更新的代码:

$this->db->trans_start();

// $where contains the array for where clause
// $data contains the array of user's data which includes the updated value

$this->db
->where($where)
->update('users', $data);

$this->db->trans_complete();

if($this->db->trans_status() === FALSE)
{
$response = array(
'message' => "Unable to edit user",
'status' => false
);
}
else
{
if($this->db->affected_rows() > 0)
{
$response = array(
'message' => "User edited successfully",
'status' => true
);
}
else
{
$response = array(
'message' => "User edited successfully, but it looks like you haven't updated anything!",
'status' => true
);
}
}


return $response;

关于php - Codeigniter - 更新查询执行成功,但 $this->db->affected_rows() 返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44884659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com