PHP error with SQLserver exception 'PDOException' with message 'SQLSTATE [08001]: [Microsoft] [ODBC Driver 11 for SQL Server]

1

I installed the driver right in PHP, I added the extension in php.ini , but when I try to connect with PDO in sqlserver, this error appears:

  

exception 'PDOException' with message 'SQLSTATE [08001]:   [Microsoft] [ODBC Driver 11 for SQL Server] Named Pipes Provider:   could not open a connection to SQL Server.

I do not know what it can be anymore, I'm using PHP version 5.5 and SQLserver express database 2014. Here is my database connection code:

    $UserName = 'sa';
    $Password = 'xxxxx';

try{
    $con = new PDO("sqlsrv:Server=localhost;Database=banco", "$UserName", "$Password");
 echo "banco conectado OK!";
}catch (PDOException $exception)
{
  die("Não é possível se conectar ao banco de dados.<br />Error message:<br /><br />$exception.");
}

This is the first time I try to connect PHP with SQLserver. If anyone can help me, thank you:)

    
asked by anonymous 14.09.2016 / 21:48

2 answers

1

I work with mysql doing updates from an MsSql database. And I've used connections with ACCESS tbm.

For the two use odbc connection.

$db = odbc_connect( $connect_string,$user, $password );

As suggested by official php documentation .

Note: Make a phinfo() to make sure your drive is active.

Then check MSSQL access credentials .

    
14.09.2016 / 23:55
1

If it is correct according to your comment, you said that the port is 1234 , so it should be something custom, because the default port of SQLServer is 1433 , to access a different port you must add a comma after the hostname, and then the port you are using:

$con = new PDO("sqlsrv:Server=[HOST],[PORTA];Database=[BANCO]", "$UserName", "$Password");

It would be something like:

$con = new PDO("sqlsrv:Server=localhost,1234;Database=banco", "$UserName", "$Password");

Like the examples in the documentation link

    
15.09.2016 / 04:43