2016-04-29 74 views
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) 

任何想法爲什麼這給我一個錯誤?

+0

' - 如果它>準備()''返回FALSE'無法準備聲明。你應該檢查錯誤。 –

+0

準備失敗,因此返回FALSE到語句變量 – RiggsFolly

+0

這就是爲什麼我有getInstance ....那就是Singleton的概念 – BRG

回答

3

看這句話在你的query()方法,

if($this->_query = $this->_pdo1->prepare($sql) || $this->_query = $this->_pdo2->prepare($sql) || $this->_query = $this->_pdo3->prepare($sql)){ ... 

的問題是,因爲邏輯OR條件錯誤分組。使用此條件語句$this->_query將始終評估爲truefalse

這裏有兩個樣品的例子來說明這一點:

要使其工作,改變條件語句是這樣的:

if(($this->_query = $this->_pdo1->prepare($sql)) || ($this->_query = $this->_pdo2->prepare($sql)) || ($this->_query = $this->_pdo3->prepare($sql))){ ... 

所以你query()方法應該是這樣的:

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"; 
     } 
    } 
} 

,並測試代碼,運行這樣一個簡單的查詢:

DB::getInstance()->query("SELECT * FROM users"); 
相關問題