9
votes

Connection to sqlsrv string not working.

$login = new PDO("sqlsrv:server=MYSQLSERVER\SQLEXPRESS;Database=db_name", "user", "passw");

And i have error message: Fatal error: Invalid handle returned.

I'm 101% sure that login details is OK. Because it works on other projects. Could be a problem PHP 7?

3
Just a couple of things to establish please; As its a named instance is it using dynamic ports that are being blocked by a local firewall? Also are you aware that SQL Express doesn't have TCP connection enabled by default. - Paul Andrew
I'm using proper SQl server but this name only for illustration. Also i just found sometimes working and sometimes return error... - Klapsius
What version of the SQLSRV library have you installed? Stable ones do not support PHP/7, you have to fetch a nightly from the GibHub repo. - Álvaro González
@ÁlvaroGonzález 4.0.4 from github.com/Azure/msphpsql/releases - Klapsius
@Klapsius I have the same issue, php7 and IIS and MSSQL 2012 ... how did you resolve ? - Simone Pessotto

3 Answers

2
votes

Looks like to connect to MS-SQL, you will need to utilize ODBC.

Reference:http://php.net/manual/en/pdo.construct.php#120705

$odbc="odbc:Driver={SQL Server};Server=$server;Database=$database;";

$cnx = new PDO( $odbc , $user, $password);
18
votes

Try "ConnectionPooling=0" in DSN.

sometimes working and sometimes return error

It might be a problem on re-using connection.

DSN Reference: https://msdn.microsoft.com/en-us/library/ff628167(v=sql.105).aspx

3
votes

There's a problem with PHP Sql Server driver
You should update it to new version

As you mentioned you are using windows, this version is fully working on windows

https://github.com/Microsoft/msphpsql/releases/tag/v4.1.1-Windows