Cannot connect to SQL Server with PHP script - php

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

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

Related

Connection Could Not be Established with SQL Server 2019

I'm having a problem running my PHP project on our IIS with SQL Server 2019. My version of PHP is 7.0.26 and I have already imported SQLSRV DLLS.
When I try to login with my system, the following error is popping up:
Connection could not be established.
Array ( [0] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user 'sa'. [message] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user 'sa'. ) [1] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user 'sa'. [message] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user 'sa'. ) )
This is some part of my connection string.
$myCon = array("Database"=>$myDB, "UID"=>$myUser, "PWD"=>$myPass);
$db = sqlsrv_connect( $myServer, $myCon);
Also, I tried to trace the error, like adding the wrong credentials to see if there is some changes on the error message but it is still the same.
We tried some configurations, like setting up the server authentication into "SQL Server and Windows Authentication Mode", firewall is disabled, and also setting up the Native client configuration.
IIS configuration was made setting MIME Type, default document, connection string, Application Pool, and all the path config was set.
I also installed only ODBC Driver 13, 2015 Redistributable, .NET 4 framework.
Can anyone help me? Thank you.
The SA account is created during the installation process. The SA account is well known and often targeted by malicious users, so it is advisable to disable the sa account unless your application requires it. you could create a new account in the SQL server or use windows authentication for accessing the SQL database.
<?php
$serverName = "serverName\\sqlexpress"; //serverName\instanceName
$connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
$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));
}
?>
Refer below link for more detail on how to create a user and configure with iis:
https://forums.iis.net/post/2159167.aspx
https://learn.microsoft.com/en-us/sql/connect/php/programming-guide-for-php-sql-driver?view=sql-server-ver15
https://learn.microsoft.com/en-us/sql/connect/php/connecting-to-the-server?view=sql-server-ver15

Wampserver Configuration with SQL Server 2017

Hello to anyone reading this. I am having a serious issue with configuration using Wampserver & SQL Server 2017. The PHP version is 7.1.9. Apache 2.4.27. The inspiration I have received was from a video made a few years ago https://www.youtube.com/watch?v=AUUh_WVzqq4 . All I am trying to do at this point is simply set up the connection to the database locally. I posted my php code and this is the error that I am getting outputted to browser...
Connection could not be established.
Array ( [0] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user 'WORKGROUP\DESKTOP-REPGNI2$'. [message] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user 'WORKGROUP\DESKTOP-REPGNI2$'. ) [1] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Cannot open database "officialdatabase" requested by the login. The login failed. [message] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Cannot open database "officialdatabase" requested by the login. The login failed. ) [2] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user 'WORKGROUP\DESKTOP-REPGNI2$'. [message] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user 'WORKGROUP\DESKTOP-REPGNI2$'. ) [3] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Cannot open database "officialdatabase" requested by the login. The login failed. [message] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Cannot open database "officialdatabase" requested by the login. The login failed. ) )
It seems to be a common error based on what I have been looking up about it. I have tried updating my drivers to the newest version ODBC Driver 13 for SQL Server TO ODBC Driver 17 for SQL Server. I have the dll files in the php.ini located in Apache (I think) and as proof see the phpinfo() screenshot. These dll files are the ones for version 7.0+ for PHP by the way. The service is running under a name "NT Service\MSSQLSERVER" and I have tried changing this to a true user in my database which is just me under the Windows Authentication (In SQL Server my Windows Authentication has privileges as well to the database). I have also tried filling in the "UID" and "PWD" fields in my connectionInfo with a user that I created as an admin. I have tried re-downloading Microsoft Visual C++ 2013 Redistributable (x64) - 12.0.30501. All of this has resulted in no success and the same error. Any idea what the problem could be?
<?php
$serverName = "Desktop-repgni2"; //serverName\instanceName
// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"officialdatabase");
$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));
}
?>

PHP: ODBC 11 does not support connections to sql server 2000 or earlier version

I'm trying to connect to MS SQL using sqlsrv_connect, before I installed ODBC 11 I got error that required to install ODBC 11 so I installed it. After i Install I got another error message again
Array
(
[0] => Array
(
[0] => 08001
[SQLSTATE] => 08001
[1] => 22
[code] => 22
[2] => [Microsoft][ODBC Driver 11 for SQL Server]ODBC Driver 11 for SQL Server does not support connections to SQL Server 2000 or earlier versions.
[message] => [Microsoft][ODBC Driver 11 for SQL Server]ODBC Driver 11 for SQL Server does not support connections to SQL Server 2000 or earlier versions.
)
[1] => Array
(
[0] => 08001
[SQLSTATE] => 08001
[1] => 22
[code] => 22
[2] => [Microsoft][ODBC Driver 11 for SQL Server]Client unable to establish connection
[message] => [Microsoft][ODBC Driver 11 for SQL Server]Client unable to establish connection
)
)
Is this possible to connect these connections?
function access_store($ips){
$conInfo = array("Database"=>"site5");
$conn = sqlsrv_connect($ips, $conInfo);
if($conn){
echo'Connection SUccess';
}else{
die('<pre>'.print_r( sqlsrv_errors(),true ).'</pre>');
}
}
What version Microsoft PHP sql driver installed? You need an older version of the driver.
Tech republic has a good article
Tech republic
Microsoft SQL server driver

Connecting to database on SQL server from WAMPSERVER 2.5

I've got a WAMPSERVER 2.5 development server. It's running PHP 5.512.
I'm trying to connect to our live SQL Server. I've installed and activated the version 32 files, and used the following code:
<?php
$link = sqlsrv_connect('obscured_ip:obscured_port', array('Database'=>'obscured_database','UID'=>'obscured_uid', 'PWD'=>'obscured pwd'));
if (!$link)
die(print_r(sqlsrv_errors(), true));
?>
I'm getting the following error:
Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP 1 => -49 [code] => -49 2 => This extension requires the Microsoft ODBC Driver 11 for SQL Server. Access the following URL to download the ODBC Driver 11 for SQL Server for x86: http://go.microsoft.com/fwlink/?LinkId=163712 [message] => This extension requires the Microsoft ODBC Driver 11 for SQL Server. Access the following URL to download the ODBC Driver 11 for SQL Server for x86: http://go.microsoft.com/fwlink/?LinkId=163712 ) 1 => Array ( [0] => IM002 [SQLSTATE] => IM002 1 => 0 [code] => 0 2 => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified [message] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified ) )
The link specified there just takes me back to the driver download page. What am I doing wrong?
The URL in the error points to the wrong page. This is the correct one: https://www.microsoft.com/en-us/download/details.aspx?id=36434

How can i connect PHP 5.5.x or above version to mssql 2008 R2 in wamp?

How can i connect PHP 5.5.x or above version to mssql 2008 R2 in wamp?
Versions :
php : 5.5
Compiler : MSVC11
Architecture : x86
Tread safety : enabled
I tried with following step :
1. Tried connecting with sqlsrv_connect()
Example code below(test-connect.php) :
<?php
$serverName = "192.168.1.5";
$connectionInfo = array( "Database"=>"testDB", "UID"=>"sa", "PWD"=>"sa123");
$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));
}
?>
When I run file test-connect.php, its throwing the following error :
**"Fatal error: Call to undefined function sqlsrv_connect()"**
I tried putting following two DLL file to PHP ext folder
a) php_pdo_sqlsrv_55_nts.dll
b) php_sqlsrv_55_nts.dll
And updated php.ini with the following extensions
extension=php_sqlsrv_55_ts.dll
extension=php_pdo_sqlsrv_55_ts.dll
Restarted all services
Still it throw below given error :-
**Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -49 [code] => -49 [2] => This extension requires the Microsoft SQL Server 2012 Native Client. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712 [message] => This extension requires the Microsoft SQL Server 2012 Native Client. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712 ) [1] => Array ( [0] => IM002 [SQLSTATE] => IM002 [1] => 0 [code] => 0 [2] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified [message] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified ) )**
Can anyone help me with fixing this? Is this the problem with versions of Apache/PHP ?
Look here :
http://www.iis.net/learn/application-frameworks/install-and-configure-php-on-iis/install-the-sql-server-driver-for-php
Basicly you need to install library which url is in your error desc and put to php.ini this library, somethin like this:
Extension=php_sqlsrv.dll

Categories