【问题标题】:Codeigniter - update query executed successfully but $this->db->affected_rows() returns false [duplicate]Codeigniter - 更新查询成功执行,但 $this->db->affected_rows() 返回 false [重复]
【发布时间】:2017-07-03 11:42:39
【问题描述】:

我正在开发一个基于 Codeigniter 的 Web 项目,我需要从管理面板更新用户。如果更新的值与数据库中已经存在的值不同,它可以正常工作。问题是 $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;

【问题讨论】:

  • 如何确定“更新查询执行成功”?
  • 会是这样吗?如果该值与数据库中的值相同,则不会更新,因此您需要更改修改时间以使其工作
  • @urfusion 我在数据库中有一个使用 ON UPDATE CURRENT_TIMESTAMP 的时间字段。所以,时间在更新。

标签: php mysql codeigniter


【解决方案1】:

您可以使用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;

【讨论】:

    【解决方案2】:

    它将返回numeric 值,而不是true 请检查使用 if($this->db->affected_rows() > 0) 请阅读文档here

    【讨论】:

    • 这不是正确的答案@metalbox,因为它不知道基本的 php。 1(更一般地说,任何非零)是一个“真实”数字,0 是一个“虚假”数字。
    【解决方案3】:

    根据 CI 文档 $this->db->affected_rows(),如果用户已编辑任何内容,则返回 true;如果查询成功执行但用户未更改数据,则返回 false。

    您只需确认查询已成功运行。如果查询成功运行,则表示您的数据已更新。

    $result = $this->db
            ->where($where)
            ->update('users', $data);
    
    if ( ! $result)
    {
         // Error
    
    }else{
    
         // Function ran ok - do whatever
             if($this->db->affected_rows() == true)
            {
                $response = array(
                 'message' => "User edited successfully",
                'status' => true
                 );
            } else {
              $response = array(
              'message' => "There is nothing to update",
               'status' => false
              );
         }
    
    }
    

    【讨论】:

    • 克里斯墨菲,你可以做一件事,直到用户没有改变任何值不要启用更新按钮。
    猜你喜欢
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多