PHP error with Sqlserver Exception 'Pdoexception' with message 'SQLSTATE[08001]: [Microsoft][ODBC Driver 11 for SQL Server]

Asked

Viewed 1,970 times

2

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

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

I don’t know what else it might be, I’m using version 5.5 of PHP and the Sqlserver express 2014 database. Follow my connection code to the database:

    $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.");
}

It’s the first time I try to connect PHP with Sqlserver. If anyone can help me, I appreciate :)

  • 1

    Which door to your SQLSERVER?

  • the password xxxxx is purposeful?

  • The door is the 1234... The password put 'xxxxx' only purposeful, just for the same question ;)

  • try to put the door

  • @stderr very well observed.

  • the error continues?

  • Opa, actually the variables were wrong, but even correcting, or even putting user and password direct, the error persists, already put the port tbm and nothing... :/

  • the name of the bank is bank?

  • Like the password, the name of the database was purposeful in the question... ;) this is the extension I added in php.ini... Extension=php_pdo_sqlsrv_55_ts.dll

Show 4 more comments

2 answers

1


I work with mysql doing updates of 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 the official php documentation.

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

Then check MSSQL access credentials.

  • 1

    Guys!!! Got it, just like the zwitterion spoke!! I used connection via ODBC, and not the way I was doing. The connection was right, I made a select via PHP to see if it was really right, and funfou correctly! the script was like this: $con = new PDO("odbc:Driver={SQL Server};Server=.\SQLEXPRESS;Database=banco; Uid=sa;Pwd=xxxxx;"); Thanks for everyone’s help, every comment was extremely helpful to me! Vlw

1

If you are correct as your comment, you said the door is 1234, then it must be something customized, 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");

Such as the examples in the documentation http://php.net/manual/en/ref.pdo-sqlsrv.connection.php#example-1097

Browser other questions tagged

You are not signed in. Login or sign up in order to post.