Error connecting to MSSQL using PHP

666 views Asked by At

I am receiving an error as below:

PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[08001]: [Microsoft][ODBC Driver 11 for SQL Server]SQL Server Network Interfaces: Connection string is not valid [87]. '

My codes are as follow:

$this->link = new PDO(
                "sqlsrv:server=$this->serverName:$this->port;Database=$this->db",
                "$this->uid",
                "$this->pwd"
                );

I wish someone can enlighten me on what is causing this error. Before that I was using dblib instead of sqlsrv.

"dblib:host=$this->serverName:$this->port;dbname=$this->db"

I am using XAMMP with Apache 2.4.12 and PHP 5.5.24 (they are all x86). I have php_pdo_sqlsrv_55_ts.dll and php_sqlsrv_55_ts.dll. I am also using Microsoft ODBC Driver 11 for SQL Server-x64.

1

There are 1 answers

0
RuubW On BEST ANSWER

Change it to:

$this->link = new PDO(
            "sqlsrv:Server={$this->serverName},{$this->port};Database={$this->db};",
            $this->uid,
            $this->pwd
            );

The default SQL Server port is 1433. Note the curly brackets, they allow for class variables.