1

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 ?

4
  • Check for $result["dbnamee"], whether it's dbnamee or dbname ? Commented Aug 31, 2015 at 17:10
  • How did it "fail"? Exception? Commented Aug 31, 2015 at 17:10
  • @raveenanigam I edited. but my issue stands still. Commented Aug 31, 2015 at 17:13
  • @Devon msg: 'PDOException' with message 'could not find driver' in C:\inetpub.. and even I set connection (for next) will it be able to transfer data with created dynamically db ? its my issue desc. thank you Commented Aug 31, 2015 at 17:20

2 Answers 2

4

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;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

This would be a better quality answer if you gave some explanation of why this code solves the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.