php与pdo,创建动态数据库连接

时间:2015-08-31 17:08:29

标签: php pdo

我使用此声明创建连接

dbh = new PDO('mysql:host=localhost;dbname=mydb;port=3306;connect_timeout=15', 'root', '');

但是在我的应用程序中,用户能够更改数据源,因此我需要使用用户发布的服务器信息创建当前数据库连接,我试试这个:

dbh = new PDO('sqlsrv:server=' + $result["servername"] + ';dbname=' + $result["dbname"] + ';port=3306;connect_timeout=15', '' + $result["user"] + '', '' + $result["password"] + '');`

但它失败了。即使我尝试了简单的代码示例,但是它也失败了。但是它是什么,我怎么能成功呢?

2 个答案:

答案 0 :(得分:3)

首先,您无法将字符串与' +'合并。你必须使用'。'你需要在这里为每个数据库连接创建新的PDO连接对象(或设置它现有)我分享我的部分代码:

private function __construct()
    {
        //default database connection
        $this->dbh = new PDO('mysql:host=localhost;dbname=webfilter;port=3306;connect_timeout=15', 'root', '');
        $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $this->dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
        $this->directed = false;
        $this->resultBool = true;
    }

    public static function getConnection()
    {
        $sqlstring = "select * from datasources WHERE ACTIVE =1";
        if (!isset(self::$instance))
        {
            $object = __CLASS__;
            self::$instance = new $object;
        }
        if (true) {
            $request = self::$instance->dbh->prepare($sqlstring);
            if ($request->execute()) {
                if ($result = $request->fetch(PDO::FETCH_ASSOC)) {
                    if ($result["SOFTWARE"] == "mysql") {
                        self::$instance->dbh = null;
                        self::$instance->connectedDbName = "mysql(" . $result["DATABASENAME"] . ")";
                        self::$instance->dbh = new PDO('mysql:host=' . $result["SERVERADDRESS"] . ';dbname=' . $result["DATABASENAME"] . ';port=3306;connect_timeout=15', '' . $result["USERNAME"] . '', '' . $result["PASSWORD"] . '');
                        self::$instance->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                        self::$instance->directed = true;
                        self::$instance->resultBool = true;
                    } else if ($result["SOFTWARE"] == "mssql") {
                        self::$instance->dbh = null;//close the existing connection
                        self::$instance->connectedDbName = "mssql(" . $result["DATABASENAME"] . ")";
                        self::$instance->dbh = new PDO('sqlsrv:server=' . $result["SERVERADDRESS"] . ';Database=' . $result["DATABASENAME"] . '', '' . $result["USERNAME"] . '', '' . $result["PASSWORD"] . '');
                        self::$instance->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                        self::$instance->dbh->query("use " . $result["DATABASENAME"]);
                        self::$instance->directed = true;
                        self::$instance->resultBool = true;
                    } else if ($result["SOFTWARE"] == "oracle") {
                        self::$instance->connectedDbName = "oracle(" . $result["DATABASENAME"] . ")";
                        self::$instance->dbh = new PDO('odbc:DRIVER=FreeTDS;server=localhost;Database=Dbname', 'username', '!123qwe');
                    }
                    self::$instance->resultBool = true;
                    $temp = self::$instance->dbh->query("select DEVICEID from ethernet LIMIT 1");
                    self::$instance->setDeviceid($temp->fetchColumn());
                    return self::$instance;
                }
                self::$instance->connectedDbName = "default";
                $temp = self::$instance->dbh->query("select DEVICEID from ethernet LIMIT 1");
                self::$instance->setDeviceid($temp->fetchColumn());
                return self::$instance;
            }
        }
        self::$instance->connectedDbName = "default";
        return self::$instance;
    }

    public function getConnectionStatusMessage()
    {
        if ($this->resultBool) {
            return "Connection Successfull" . $this->connectedDbName;
        } else {
            return "Connection Failed:" . $this->resultString;
        }
    }

答案 1 :(得分:0)

$dbh = new PDO('sqlsrv:server=' . $result["servername"] . ';dbname=' . $result["dbname"] . ';port=3306;connect_timeout=15', $result["user"], $result["password"]);