简体   繁体   中英

Connecting PDO to MS SQL Server 2008 R2 Named Pipes Provider: Could not open a connection to SQL Server [53]

My PC has Installed MS SQL Server 2008 R2 with installed Database, Then I installed XAMPP and made another port for Apache to make it localhost:8080 and listen to 4433 . Then Installed OBDC , Tried to connect using

try {
$host = 'LOCAL';
$user = '****';
$pass = '****';

$dsn = "sqlsrv:Server=$host;Database=$db;";
$opt = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION
];
$pdo = new PDO($dsn, $user, $pass, $opt);

} catch (PDOException $e) {
    echo "No connection: " . $e->getMessage();
    exit;
}

But I keep getting

No connection: SQLSTATE[08001]: [Microsoft][ODBC Driver 13 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. 

Used extension=php_pdo_sqlsrv_71_ts_x86.dll

I've shutdown Windows Firewall , Enabled Pipe Connection in MS SQL 2008 Configuration and Internet Option NetBIOS Enabeled

$host shouldn't be "LOCAL" but "localhost" .

If you are running SQL server on a custom port on your local machine, you can specify the port by appending it with a comma to the host; applied to your code snippet:

$host = "localhost";
// or, with a custom port
$host = "localhost,12345"

Source: http://php.net/manual/en/ref.pdo-sqlsrv.connection.php#refsect1-ref.pdo-sqlsrv.connection-examples

if you are using a local instance, the server string needs a double backslash:

<?php
$conn = new PDO('sqlsrv:Server=localhost\\SQLEXPRESS;Database=MyDatabase', 'MyUsername', 'MyPassword');
?>

See original provided answer by Daniel Klein: https://www.php.net/manual/en/ref.pdo-sqlsrv.connection.php#121301

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