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));
}
?>
Related
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
Error to connect MSSQL database through PHP
I can't able to connect to sql server database in remote system. It is showing Unable to connect to your database server using the provided settings.
Filename:
returns the bellow error..
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 'NT AUTHORITY\IUSR'. [message] =>
[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for
user 'NT AUTHORITY\IUSR'. ) [1] => Array ( [0] => 42000 [SQLSTATE] =>
42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][ODBC Driver 13 for
SQL Server][SQL Server]Cannot open database "pestcontrol" requested by
the login. The login failed. [message] => [Microsoft][ODBC Driver 13
for SQL Server][SQL Server]Cannot open database "pestcontrol"
requested by the login. The login failed. ) )
This can be solve your problem
$db['default']['username'] = 'root';
$db['default']['password'] = '';
It is by default setting in xampp, lampp and wampp. If you want create your own credential you can write it in the code given.
or try this link:
https://www.c-sharpcorner.com/UploadFile/specialhost/how-to-fix-login-failed-for-user-nt-authority-iusrs-in-share/
I can connect to sql server remotely. The connection string is:
Provider=SQLOLEDB.1; Persist Security Info=True; Data Source=3.120.12.12;
Initial Catalog=StoreDB; User ID=mohan; PASSWORD=123456
I used of sqlsrv_connect to connect:
$serverName = "3.120.12.12";
$connectionOptions = array
(
"Database" => "StoreDB",
"Uid" => "mohan",
"PWD" => "123456"
);
$conn = sqlsrv_connect($serverName, $connectionOptions);
if($conn) echo "Connected!"; else die( print_r( sqlsrv_errors(), true));
But this doesn't connect to the database and show's the following error:
Array
(
[0] => Array
(
[0] => HYT00
[SQLSTATE] => HYT00
[1] => 0
[code] => 0
[2] => [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired
[message] => [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired
)
[1] => Array
(
[0] => 08001
[SQLSTATE] => 08001
[1] => 258
[code] => 258
[2] => [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x102
[message] => [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x102
)
[2] => Array
(
[0] => 08001
[SQLSTATE] => 08001
[1] => 258
[code] => 258
[2] => [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
[message] => [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
))
After this error, I change $serverName to "3.120.12.12:1433" and it then shows this error:
Array ( [0] => Array ( [0] => HYT00 [SQLSTATE] => HYT00 [1] => 0 [code] => 0 [2] => [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired [message] => [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired ) [1] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 87 [code] => 87 [2] => [Microsoft][ODBC Driver 17 for SQL Server]MAX_PROVS: Connection string is not valid [87]. [message] => [Microsoft][ODBC Driver 17 for SQL Server]MAX_PROVS: Connection string is not valid [87]. ) [2] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 87 [code] => 87 [2] => [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. [message] => [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. ) )
However, I can connect to the database by "Microsoft sql server management studio" without problem.
How can connect to database using sqlsrv_connect ?
Its either the firewall or any additional security features running on your system like selinux (if you are on Red Hat).
If you do have the selinux kernel check for the booleans and see if the httpd_can_network_connect_db is off with
getsebool -a. If it is of you need to turn it on. You can do that using this command: setsebool -P httpd_can_network_connect_db 1
And don't forget to restart your server (apache or nginx)
more info about this command here:
https://linux.die.net/man/8/setsebool
This question already has an answer here:
Connecting to SQL Server with sqlsrv_connect using Windows authentication
(1 answer)
Closed 5 years ago.
I installed mssql and it shows enaabled in phpinfo(). But when setting up joomla on database config page when I choose mssql and try to connect the following error comes up
Could not connect to the database. Connector returned number: Database sqlsrv_connect failed, Array ( [0] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'user'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'user'. ) [1] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'user'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'user'. ) )
Tried putting user root Admin but does not work. How can I get this page working?
Typically, this problem happens when MSSQL server authentication is not enabled in the local server. You can enable that the following way:
Go to MSSQL and then right click
Click on "Properties" and then on "Security"
Click on "Server Authentication"
Enable Authentication Mode
I'm running a PHP site using IIS and connecting to a Sql Server server on another computer on the network. I created a domain user that has access to the db and configured the app pool to use that domain user.
I keep getting this error:
Unable to connect.
Array
(
[0] => Array
(
[0] => 28000
[SQLSTATE] => 28000
[1] => 18456
[code] => 18456
[2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.
[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.
)
[1] => Array
(
[0] => 28000
[SQLSTATE] => 28000
[1] => 18456
[code] => 18456
[2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.
[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.
)
)
Not sure why it's using user 'NT AUTHORITY\ANONYMOUS LOGON' instead of the domain user in the application pool.
Thanks
I got this working by clicking on Websites in IIS, then double click on Authentication, and right click on Anonymous Authentication->Edit, and select the Application Pool Identity.
1.) Install System Internals Process Monitor, if not already installed.
https://technet.microsoft.com/en-us/sysinternals/bb842062
2.) Check the monitoring logs to see exactly where the anonymous login is occurring.
That will allow you to diagnose exactly what's going on.