【问题标题】:Displaying false result, while the query returned true显示假结果,而查询返回真
【发布时间】:2018-11-11 06:29:18
【问题描述】:

我正在尝试执行以下代码。

执行成功,但显示sorry, Query could not execute...

我的代码:

class.php

public function admin_update($id,$update,$value)
    {
        try{
            $stmt = $this->conn->prepare("UPDATE admin SET ".$update."=:_value WHERE Id=:_id");
            $stmt->bindparam(":_value",$value);
            $stmt->bindparam(":_id",$id);
            $stmt->execute();
        }
        catch(PDOException $ex)
        {
            echo $ex->getMessage();
        }
    }

update.php

$id = "1";

if(admin_update($id,"Access","Y"))
        {               
            echo "Success";
        }
        else
        {
            echo "sorry, Query could not execute...";
        }

MySQL 表已更新,但没有显示Success,而是显示sorry, Query could not execute...

【问题讨论】:

    标签: php mysqli pdo sql-update


    【解决方案1】:

    您需要return execute() 的结果。由于您没有返回任何内容,因此默认情况下将其视为null,因此您的if() 条件始终将其检查为false,并转到else() 部分。

    public function admin_update($id,$update,$value)
        {
            try{
                $stmt = $this->conn->prepare("UPDATE admin SET ".$update."=:_value WHERE Id=:_id");
                $stmt->bindparam(":_value",$value);
                $stmt->bindparam(":_id",$id);
                return $stmt->execute(); // return the result of execute
            }
            catch(PDOException $ex)
            {
                echo $ex->getMessage();
                return 0; // return 0 in case of failure
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-14
      • 2013-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-29
      • 1970-01-01
      相关资源
      最近更新 更多