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.

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

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题