1
我在這裏遇到了PDO :: execute()的問題。問題是我有3個數據庫,我想要構建一個Singleton類和一個通用查詢方法。所以,我的DB類的結構是這樣的:致命錯誤布爾型執行()
class DB{
private static $_instance = null;
private $_pdo1, $_pdo2, $_pdo3,
$_query,
$_results,
$_error = false,
$_count = 0;
private function __construct(){
try{
$dns1 = 'mysql:host='.Config::get('mysql1/host').';dbname='.Config::get('mysql1/dbname').'';
$dns2 = 'mysql:host='.Config::get('mysql2/host').';dbname='.Config::get('mysql2/dbname').'';
$dns3 = 'mysql:host='.Config::get('mysql3/host').';dbname='.Config::get('mysql3/dbname').'';
$username1 = Config::get('mysql1/username');
$username2 = Config::get('mysql2/username');
$username3 = Config::get('mysql3/username');
$password1 = Config::get('mysql1/password');
$password2 = Config::get('mysql2/password');
$password3 = Config::get('mysql3/password');
$this->_pdo1 = new PDO($dns1, $username1, $password1);
$this->_pdo2 = new PDO($dns2, $username2, $password2);
$this->_pdo3 = new PDO($dns3, $username3, $password3);
}catch(PDOException $e){
die($e->getMessage());
}
}
public static function getInstance(){
if(!isset(self::$_instance)){
self::$_instance = new DB();
}
return self::$_instance;
}
public function query($sql, $params = array()){
$this->_error = false;
if($this->_query = $this->_pdo1->prepare($sql) || $this->_query = $this->_pdo2->prepare($sql) || $this->_query = $this->_pdo3->prepare($sql)){
if(count($params)){
$x = 1;
foreach($params as $param){
$this->_query->bindValue($x, $param);
$x++;
}
}
if($this->_query->execute()){
echo "Success";
}
}
}
}
在49線我得到這個錯誤:
Fatal error: Call to a member function execute() on boolean in /htdocs/DatabaseStructure/classes/DB.php on line 49
線49:if($this->_query->execute())
也試過,但沒有成功:
if($this->_query->execute() == TRUE)
if(!$this->_query->execute() == FALSE)
任何想法爲什麼這給我一個錯誤?
' - 如果它>準備()''返回FALSE'無法準備聲明。你應該檢查錯誤。 –
準備失敗,因此返回FALSE到語句變量 – RiggsFolly
這就是爲什麼我有getInstance ....那就是Singleton的概念 – BRG