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() {
}
}
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.