PDO连接到多个数据库

时间:2013-09-02 11:49:38

标签: php mysql database pdo singleton

我有以下PDO课程:

class DB {

        private $dbh;
        private $stmt;

        static $db_type;
        static $connections;

        public function __construct($db, $id="") {

             switch($db) {
                case "db1":
                      try{

                          $this->dbh = new PDO("mysql:host=localhost;dbname=ms".$id, 'root', '', array( PDO::ATTR_PERSISTENT => true ));
                      }  catch(PDOException $e){
                          print "Error!: " . $e->getMessage() . "<br />";
                          die();
                      }
                break;
                case "db2":
                      try{
                          $this->dbh = new PDO("mysql:host=localhost;dbname=users", 'root', '', array( PDO::ATTR_PERSISTENT => true ));
                      } catch(PDOException $e){
                          print "Error!: " . $e->getMessage() . "<br />";
                          die();
                      }
                break;
            }
            self::$db_type = $db;
        }

        static function init($db_type = ""){ 

            print_r(self::$connections);


            if(!isset(self::$connections[$db_type])){ 
                self::$connections[$db_type] = new self($db_type); 
            } 

            return self::$connections[$db_type];
        }

        public static function query($query) {

            self::$connections[self::$db_type]->stmt = self::$connections[self::$db_type]->dbh->prepare($query);
            return self::$connections[self::$db_type];
        }

        public function bind($pos, $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;
                }
            }

            self::$connections[self::$db_type]->stmt->bindValue($pos, $value, $type);
            return self::$connections[self::$db_type];
        }

        public function execute() {
            return self::$connections[self::$db_type]->stmt->execute();
        }
    }

接下来我试着:

$id = 1;
DB::init('db1', $id);

这给我一个错误:

Error!: SQLSTATE[HY000] [1049] Unknown database 'ms'<br />

为什么我的数据库名称在连接期间为ms,因为它应该是ms1?感谢。

1 个答案:

答案 0 :(得分:2)

您的init方法的签名是:

 static function init($db_type = "")

这意味着它只接受一个参数,而你这样称呼它:

DB::init('db1', $id);

这不起作用。另外:你需要阅读static,持久连接和注入与单身......你的代码充满了问题。首先:始终指定访问修饰符 你的标题建议想要使用多个连接,但是你要一遍又一遍地重新分配$db_type属性(它是静态的,所以在所有实例上共享)。
你试图使用Singleton模式,这在PHP中毫无意义,但在你的情况下更是如此,因为你的构造函数是公共的,仍然...

如果必须,只使用静态,即便如此:仔细思考:大多数情况下,必须使用静态意味着必须承认设计错误。

query方法只接受1个参数:一个字符串,并对上次建立的连接执行该查询。您无法选择此查询将在哪个数据库上运行。如果不是危险的,那么,我不想成为一个蠢货,但我不能用其他任何方式:这是可怕的代码

请重构这段代码,如果我发现自己必须使用这个类,我只需要创建自己的PDO实例并使用它。无论你如何看待它:你明显限制了一个人可以执行的查询,但是你无法拒绝我对PDO本身的访问......