2016-04-29 39 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"); 
相关问题