PDO间歇性错误“在非对象上调用成员函数execute()”

时间:2013-01-22 19:36:15

标签: php pdo

所以我在调用PDO :: execute()时遇到这种间歇性错误。错误是“在非对象上调用成员函数execute()”。它发生在我的db类的第117行。这是我下面的课程。当我刷新此错误时页面加载正常然后间歇性地返回。

class db extends PDO {
    private $error;
    private $sql;
    private $bind;
    private $errorCallbackFunction;
    private $errorMsgFormat;

    public function __construct($dsn, $user="", $passwd="") {
        $options = array(
            PDO::ATTR_PERSISTENT => false, 
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        );

        try {
            parent::__construct($dsn, $user, $passwd, $options);
        } catch (PDOException $e) {
            $this->error = $e->getMessage();
        }
    }

    private function cleanup($bind) {
        if(!is_array($bind)) {
            if(!empty($bind)) {
                $bind = array($bind);
            }
            else {
                $bind = array();
            }
        }
        return $bind;
    }

    public function run($sql, $bind="") {
        $this->sql = trim($sql);
        $this->bind = $this->cleanup($bind);
        $this->error = "";

        try {
            $pdostmt = $this->prepare($this->sql);
            if($pdostmt->execute($this->bind) !== false) {
                if(preg_match("/^(" . implode("|", array("select", "describe", "pragma")) . ") /i", $this->sql)) {
                    return $pdostmt->fetchAll(PDO::FETCH_ASSOC);
                }
                elseif(preg_match("/^(" . implode("|", array("delete", "insert", "update")) . ") /i", $this->sql)) {
                    return $pdostmt->rowCount();
                }
            }   
        } catch (PDOException $e) {
            $this->error = $e->getMessage();    
            $this->debug();
            return false;
        }
    }

    public function select($table, $where="", $bind="", $fields="*") {
        $sql = "SELECT " . $fields . " FROM " . $table;
        if(!empty($where)) {
            $sql .= " WHERE " . $where;
        }
        $sql .= ";";

        return $this->run($sql, $bind);
    }

}   

现在每当我将PDO :: ATTR_PERSISTENT更改为false时,问题就会消失,所以看起来我的构造调用运行时会出现问题。

这也是我的模型,错误出现了。                 

class QuickView extends Model {

function getProduct($sku) {
    $bind = array(
        ":sku" => "$sku"
    );
    $result = $this->Quickview->db->select('`PRODUCTS`', 'PR_SKU = :sku', $bind, 'PR_SKU, PR_URLofImage, PR_UnitPrice, PR_Description');

    return $result;
}

}

然后这是它扩展的类。

class Model extends DB {
    protected $_model;

    function __construct() {

        global $inflect;

        $this->db = new DB("mysql:host=localhost;dbname=dbname", "username", "password");
        $this->_limit = PAGINATE_LIMIT;
        $this->_model = get_class($this);
        $this->_table = strtolower($this->_model)."s";
    }

    function __destruct() {
    }

}

1 个答案:

答案 0 :(得分:2)

查看PDO::prepare的文档:http://us1.php.net/manual/en/pdo.prepare.php

它并不总是返回PDOStatement个对象。如果处理查询时出错,它将返回false或根据其错误处理模式抛出异常。

在您的情况下,您尝试将其设置为抛出异常,但这不是驱动程序选项。它使用PDO :: setAttribute设置,即:

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

如果您解决了这个问题,您将从错误中获得异常。否则,您也可以在致电PDO::prepare之前检查execute的结果来解决问题。