简体   繁体   中英

PHP - PDO SQLSRV - Connection string is not valid [87]

I´m trying to connect to a SQL Server 2008 R2 via PHP. I used the following code:

try {
    $server = "servername.do.ma.in\\MSSQLSERVER";
    $user = "username";
    $pass = "password";
    $db = "databasename";

    $dbh= new PDO('sqlsrv:Server = ' . $server . '; Database = ' . $db, $user, $pass);

    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo 'Verbindung fehlgeschlagen: ' . $e->getMessage();
}

But when i run this code i get the following error:

 SQLSTATE[08001]: [Microsoft][ODBC Driver 11 for SQL Server]SQL Server Network Interfaces: Connection string is not valid [87]. 

Does someone know this error and know what to do?

Thanks in advance.


Solution:

The Port was not defined. The following code worked for me. I´ve added the Port 1433 to the $server Variable.

$server = "servername.do.ma.in, 1433";
    $user = "username";
    $pass = "password";
    $db = "databasename";

    $dbh = new PDO("sqlsrv:Server={$server};Database={$db};", $user, $pass);

This will work:

$dbh = new PDO("sqlsrv:Server={$server};Database={$db};", $user, $pass);

If it's still not valid, there's something wrong with your connection variables.

EDIT :

This looks similar to your problem:

http://laravel.io/forum/01-20-2015-sql-server-2012-db-connection-with-laravel-42

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM