I run SQL Server 2016. I try to connect to it via a PHP script (PHP version 8). I did install the drivers and added the path in the php.ini (same version as my PHP version):
...
extension=pdo_sqlsrv_80_nts
extension=pdo_sqlsrv_80_ts
extension=sqlsrv_80_nts
extension=sqlsrv_80_ts
...
Here is my script:
$serverName = "<ServerName>";
$connectionInfo = array( "Database"=>"<database>", "UID"=>"<user>", "PWD"=>"<pwd>");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
I get the following error:
Array
(
[0] => Array
(
[0] => IM006
[SQLSTATE] => IM006
[1] => 0
[code] => 0
[2] => [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr failed
[message] => [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr failed
)
[1] => Array
(
[0] => 01000
[SQLSTATE] => 01000
[1] => 5701
[code] => 5701
[2] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Changed database context to '<database>'.
[message] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Changed database context to '<database>'.
)
[2] => Array
(
[0] => 01000
[SQLSTATE] => 01000
[1] => 5703
[code] => 5703
[2] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Changed language setting to us_english.
[message] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Changed language setting to us_english.
)
)
Any idea what could be wrong?
Thank you for your help
3
Answers
I had the same issue after upgrading from Php 7 to 8. My PDO code (pdo_sqlsrv) still worked, as some of the commenters here mention. But the old non-PDO code (sqlsrv) got the same error that you describe.
I started switching that code over to PDO, and will continue that when I can. But I then realized that updating my SQL Server drivers resolved the issue. You can find those drivers by googling "Download ODBC Driver for SQL Server". You will be wanting verion 17 or above.
I recently stumbled upon this same issue. For me sqlsrv_query was throwing warnings as errors. I fixed this by putting
sqlsrv_configure('WarningsReturnAsErrors',0);
just above the query code.Update:
You need to update your ODBC driver’s version. According to this article, there are known issues with driver below 17.4.2.
Download latest ODBC
here.
Update 2:
If you are using PHP driver 5.9, then ODBC driver 18 will be incompatible. Refer here. You must have ODBC driver 17(higher then 17.4.2) as well.
Been stuck on this exact issue for days until the keywords "upgrade odbc driver" hit me. As soon as I’ve upgraded, the issue was fixed. https://learn.microsoft.com/en-us/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-ver15