所以我在调用 PDO::execute() 时遇到了这个间歇性错误。错误是“在非对象上调用成员函数 execute()”。它发生在我的数据库类的第 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() {
}
}