PHP PDO Fatal error: Call to a member function prepare() on null
PHP PDO Fatal error: Call to a member function prepare() on null
试图了解 PDO 的来龙去脉,但我却陷入了困境。
我当前的 PDO class 如下:
class SimpleDatabase extends PDO {
const DB_HOST='localhost';
const DB_USER='claudio';
const DB_PASS='claudio';
const DB_NAME='simpledb';
private $dbh;
private $error;
private $stmt;
public function __construct(){
// Set DSN
$dsn = 'mysql:host=' . self::DB_HOST . ';dbname=' . self::DB_NAME;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
try{
$this->dbh = new PDO($dsn, self::DB_USER, self::DB_PASS, $options);
}
// Catch any errors
catch(PDOException $e){
$this->error = $e->getMessage();
}
}
public function __destruct(){
// Adding null connection
$this->dbh = null;
$this->isConnected = false;
}
// The query, prepare, bind and execute methods allows us to use prepared statements in our DB interactions
// Prepare
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
// Bind
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
// Execute
public function execute(){
return $this->stmt->execute();
}
我确实有其余的功能,但是对于这个错误,我认为没有必要向您展示其余的功能。
然后我这样调用函数:
$database = new SimpleDatabase();
$database->query('INSERT INTO mytable (FName, LName, Age, Gender) VALUES (:fname, :lname, :age, :gender)');
$database->bind(':fname', 'John');
$database->bind(':lname', 'Smith');
$database->bind(':age', '24');
$database->bind(':gender', 'male');
$database->execute();
我收到以下错误:致命错误:在 null 上调用成员函数 prepare(),当我调用 prepare 函数时会发生这种情况。知道为什么会这样吗?我该如何解决这个问题?
看起来您的代码是 "swallowing" 尝试连接数据库并失败时引发的 PDO 异常。
这里:
catch(PDOException $e){
$this->error = $e->getMessage();
}
如果出现异常,您可以将某些内容分配给 error
成员。凉爽的。但如果连接失败,$this->dbh
可能会为空。
但除此之外,您的代码看起来好像一切正常。
就好像您的代码将小指放在嘴角,Dr.Evil 风格,然后说 "I just assume everything will go to plan, what?"
试图了解 PDO 的来龙去脉,但我却陷入了困境。
我当前的 PDO class 如下:
class SimpleDatabase extends PDO {
const DB_HOST='localhost';
const DB_USER='claudio';
const DB_PASS='claudio';
const DB_NAME='simpledb';
private $dbh;
private $error;
private $stmt;
public function __construct(){
// Set DSN
$dsn = 'mysql:host=' . self::DB_HOST . ';dbname=' . self::DB_NAME;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
try{
$this->dbh = new PDO($dsn, self::DB_USER, self::DB_PASS, $options);
}
// Catch any errors
catch(PDOException $e){
$this->error = $e->getMessage();
}
}
public function __destruct(){
// Adding null connection
$this->dbh = null;
$this->isConnected = false;
}
// The query, prepare, bind and execute methods allows us to use prepared statements in our DB interactions
// Prepare
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
// Bind
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
// Execute
public function execute(){
return $this->stmt->execute();
}
我确实有其余的功能,但是对于这个错误,我认为没有必要向您展示其余的功能。
然后我这样调用函数:
$database = new SimpleDatabase();
$database->query('INSERT INTO mytable (FName, LName, Age, Gender) VALUES (:fname, :lname, :age, :gender)');
$database->bind(':fname', 'John');
$database->bind(':lname', 'Smith');
$database->bind(':age', '24');
$database->bind(':gender', 'male');
$database->execute();
我收到以下错误:致命错误:在 null 上调用成员函数 prepare(),当我调用 prepare 函数时会发生这种情况。知道为什么会这样吗?我该如何解决这个问题?
看起来您的代码是 "swallowing" 尝试连接数据库并失败时引发的 PDO 异常。
这里:
catch(PDOException $e){
$this->error = $e->getMessage();
}
如果出现异常,您可以将某些内容分配给 error
成员。凉爽的。但如果连接失败,$this->dbh
可能会为空。
但除此之外,您的代码看起来好像一切正常。
就好像您的代码将小指放在嘴角,Dr.Evil 风格,然后说 "I just assume everything will go to plan, what?"