【问题标题】:Why does "$this->_query->execute()" always evaluate as false?为什么“$this->_query->execute()”总是评估为假?
【发布时间】:2014-03-14 17:42:13
【问题描述】:

我在 db.php 文件中有以下代码:

    public function query($sql, $params = array()){
    $this->_error = false;
    if($this->_query = $this->_pdo->prepare($sql)){
        $x = 1;
        if(count($params)){
            foreach($params AS $pa){
                $this->_query->bindValue($x, $pa);
                $x++;
            }
        }
        #MASSIVE ERROR
        #correctthis
        if($this->_query->execute()){
            echo 'success';
            $this->_result = $this->_query->fetchAll(PDO::FETCH_OBJ);
            $this->_query = $this->_query->rowCount();
        } else {
            $this->_error = true;
        }
    }

    return $this;
}

public function error(){
    return $this->_error;
}

我的问题是$this->_query->execute() 总是为假,这意味着 $_error 总是被设置为真。我无法让它工作,即使它似乎没有任何问题。

脚本进入prepare(我通过echoing测试过),表示prepare成功。它还进入 foreach 循环,因此必须绑定这些值。但它似乎无法执行。为什么?

编辑:

我使用DB::getInstance()->query("SELECT email FROM user_credentials WHERE user_id = ?", array(1)) 调用查询,getInstance 是这样的:

    public static function getInstance(){
    if(!isset(self::$_instance)){
        self::$_instance = new DB();
    }
    return self::$_instance;
}

【问题讨论】:

  • 你的 $sql 语句是什么样的?它是否期望您在执行中未提供的参数?
  • 我正在使用DB::getInstance()->query("SELECT email FROM user_credentials WHERE user_id = ?", array(1));,我正在编辑我的帖子以显示 getInstance 的样子。
  • 对于问号,您不需要绑定参数,但无论哪种方式,您都应该在执行调用中提供参数。

标签: php mysql object pdo


【解决方案1】:

我认为您没有正确连接到数据库。查看连接功能是否正常。如果您从在线教程中尝试此代码,则类似于下面的代码:

$this->_pdo = new PDO('mysql:host=' . Config::get 
('mysql/host') . ';dbname=' . Config::get('mysql/db'), 
Config::get('mysql/username'), Config::get('mysql/password')); 

【讨论】:

    【解决方案2】:

    以这种方式制作你的函数

    public function queryAll($sql, $params = array(), $method = PDO::FETCH_OBJ)
    {
        $stmt = $this->_pdo->prepare($sql);
        $stmt->execute($params);
        return $stmt->fetchAll($method);
    }
    

    或者,如果你更喜欢方法链,那么就这样

    public function query($sql, $params = array())
    {
        $stmt = $this->_pdo->prepare($sql);
        $stmt->execute($params);
        return $stmt;
    }
    

    然后将 PDO 设置为异常模式,您将始终被告知发生任何错误。为此,在构造函数中添加此代码

    $this->_pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    

    您的其余代码无用、不灵活、容易出错,有时甚至完全错误。

    【讨论】:

    • 谢谢!但是当 '$params' 没有元素时,你的代码会发生什么?另外,为什么你建议使用 '$stmt' 而不是 '$this->_query'?
    • 我不知道谁对你投了反对票,但这个答案很有帮助。我刚刚开始使用 PDO,此代码来自教程,因此您可以逐步了解数据发生了什么。
    • @markovchain 试试看。不过没什么特别的 - 查询只是像往常一样执行。
    • @markovchain $method = PDO::FETCH_OBJ 是函数manual的默认值
    【解决方案3】:

    试试这个可以知道查询返回的具体错误。我只修改了 else 块。

        #MASSIVE ERROR
        #correctthis
        if($this->_query->execute()){
            echo 'success';
            $this->_result = $this->_query->fetchAll(PDO::FETCH_OBJ);
            $this->_query = $this->_query->rowCount();
        } else {
            $this->_error = true;
            echo $this->_query->errorInfo()[2];
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多