Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 到多个数据库的PDO连接_Php_Mysql_Database_Pdo_Singleton - Fatal编程技术网

Php 到多个数据库的PDO连接

Php 到多个数据库的PDO连接,php,mysql,database,pdo,singleton,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":

我的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();
        }
    }
这将返回一个错误:

Error!: SQLSTATE[HY000] [1049] Unknown database 'ms'<br />
错误!:SQLSTATE[HY000][1049]未知数据库“ms”

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

您的init方法的签名是:

 static function init($db_type = "")
这意味着它只接受一个参数,而您这样称呼它:

DB::init('db1', $id);
这不行。另外:您还需要阅读关于
静态
、持久连接和注入与单例的比较。。。你的代码充满了问题。首先:始终指定访问修饰符。
您的标题建议您使用多个连接,但您正在反复重新分配
$db_type
属性(它是静态的,因此在所有实例中都是共享的)。
您试图使用
Singleton
模式,这在PHP中是毫无意义的,但在您的情况下更是如此,因为您的构造函数是公共的,所以仍然

只有在必要时才使用静态,即使这样:仔细想想:大多数时候,必须使用静态意味着必须承认设计错误

query
方法只接受一个参数:字符串,并在上次建立的连接上执行该查询。您无法选择此查询将在哪个数据库上运行。如果不是很危险的话,那就比这更危险了,我不想成为一个傻瓜,但我不能把它放在任何其他方面:这是一个糟糕的代码


请重构这段代码,如果我发现自己不得不使用这个类,我会创建自己的
PDO
实例并使用它。不管你怎么看:你明显地限制了一个人可以执行的查询,但是你不能拒绝我对
PDO
本身的访问…

为什么你有这么多数据库?你的
DB::init()
方法只需要一个参数。您一定调用了错误的方法(不应该是构造函数吗?)将其更改为静态函数init($db_type=“”,$id=“”){if(!isset(self::$connections[$db_type]){self:$connections[$db_type]=new self($db_type,$id);}返回self:$connections[$db_type];}@user889349:大约一百个。。。抱歉:将此问题粘贴到@user889349您最大的问题是枚举数据库,它散发出一些不寻常和不可靠的设计味道。请看:
DB::init('db1', $id);