ncksllvn
ncksllvn

Reputation: 5769

Error connecting to MSSQL with SQLSrv and PHP 5.4.7

I'm trying to connect to an MSSQL database with PHP and encounter a very frustrating error while trying to connect. It loads for a minute, before printing out the following message:

    Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[08001]: 
[Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [53]. 

Here's my connection string:

DB::config("sqlsrv:Server={$dbHost} ; Database={$dbName}", $dbUser, $dbPass);

And in my DB class:

public static function config($dsn, $user, $pass)
    {
        self::$_pdo = @new PDO($dsn, $user, $pass);
        ...// intentionally left out

I'm very baffled. I have the following module configured in my php.ini file: extension=php_pdo_sqlsrv_54_ts.dll. Although I think I installed it right and I don't receive any error message about the driver not working, this bit of PHP prints nothing:

if (function_exists( 'sqlsrv_connect' ))
    echo "hey";

and as you can see from my error message above SQL Server Native Client 11 appears to be working. I'm on a Windows platform. I tried disabling the firewall to no avail. I don't have much information about the SQL server or I would share it here.

This is my first time posting on Stack, so I apologize if I'm leaving out a key piece of information or I'm not following proper etiquette in some way, but any help is definitely appreciated.


Also, I should add that I've already done too much Googling, and haven't had any success.

Upvotes: 2

Views: 18076

Answers (2)

Rick Savoy
Rick Savoy

Reputation: 341

I ran into the same problem but I found that I did not need to use the IP address, the server name worked just fime. Also I was not sure what port my server was using, I found the information here: How to find the port for MS SQL Server 2008?

I also found the following links helpful in getting PDO to work:

PHP PDO Connection to SQL Server with integrated security?

Use of PDO in classes

MS SQL Server (PDO) — Microsoft SQL Server Functions (PDO_SQLSRV)

I know ncksllvn solved his problem so I hope this helps whoever else visits!

Upvotes: 1

ncksllvn
ncksllvn

Reputation: 5769

My install process actually worked, but the connection was indeed invalid. I should have been using the IP address as the host name, which I was not. I was also using a colon instead of a comma before the port number. Finally, no spaces can be allowed in the connection string, which I thought would be automatically stripped out. So, here's a valid connection string for those of you that may find yourself in a similar predicament:

$connectionString="sqlsrv:Server=123.123.12.1,9864;Database=mssqldatabaseWootWoot";

Also, in response to my earlier observation about that function sqlsrv_connect, I had not enabled the driver necessary for that function. It is located in php_sqlsrv_54_ts.dll, which after enabling, reported the function as existing.

Thanks to anyone who put any time into solving this.

Upvotes: 6

Related Questions