I create connection with this statement
dbh = new PDO('mysql:host=localhost;dbname=mydb;port=3306;connect_timeout=15', 'root', '');
But in my application users capable to change data sources so I need to create current database connection with server informations user posted, I try this:
dbh = new PDO('sqlsrv:server=' + $result["servername"] + ';dbname=' + $result["dbname"] + ';port=3306;connect_timeout=15', '' + $result["user"] + '', '' + $result["password"] + '');`
But it failed. Even I tried the simple code examples got documents it fails for that too.. Whats with it and how can I make it ?
At first, you cant merge strings with '+'. you must use '.' and you need to create new PDO connection object(or set it existing) for each database connection here I share parts of my code doing this:
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;
}
}
$dbh = new PDO('sqlsrv:server=' . $result["servername"] . ';dbname=' . $result["dbname"] . ';port=3306;connect_timeout=15', $result["user"], $result["password"]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.