duanpu6319 2013-01-22 19:36
浏览 77
已采纳

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

So I am getting this intermittent error on a call to PDO::execute(). Error is "Call to a member function execute() on a non-object". It happens at line 117 in my db class. Here is my class below. Page loads in fine and then intermittently when I refresh this error comes back.

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);
    }

}   

Now whenever I change PDO::ATTR_PERSISTENT to false the problem goes away so it looks to be an issue with my construct call running over itself.

Also here is my my model that the error is showing up on.

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;
}

}

And then here is the class it extends.

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条回答 默认 最新

  • dongzipu7517 2013-01-22 19:56
    关注

    Check the docs for PDO::prepare: http://us1.php.net/manual/en/pdo.prepare.php

    It does not always return a PDOStatement object. If there is an error processing the query, it will either return false or throw an exception depending on what its error handling mode is.

    In your case, you are trying to set it to throw an exception, but that is not a driver option. It is set with PDO::setAttribute, i.e:

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

    If you fix that, you will get the exception from your error. Otherwise, you can also fix it by checking the result of PDO::prepare before you call execute on it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像