Unable to connect to my mssql database using PHP - php

I see that another user had a similar question: Why can't I connect to my mssql database using PHP?
I am getting identical errors.
Array
(
[0] => Array
(
[0] => 28000
[SQLSTATE] => 28000
[1] => 18456
[code] => 18456
[2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'userName'.
[message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'userName'.
)
[1] => Array
(
[0] => 42000
[SQLSTATE] => 42000
[1] => 4060
[code] => 4060
[2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open database "DBname" requested by the login. The login failed.
[message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open database "DBname" requested by the login. The login failed.
)
[2] => Array
(
[0] => 28000
[SQLSTATE] => 28000
[1] => 18456
[code] => 18456
[2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'userName'.
[message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'userName'.
)
[3] => Array
(
[0] => 42000
[SQLSTATE] => 42000
[1] => 4060
[code] => 4060
[2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open database "DBname" requested by the login. The login failed.
[message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open database "DBname" requested by the login. The login failed.
)
)
I've succeeded in displaying phpinfo() for my page.
Looks like I'm running MSVC9 (Visual C++ 2008) (not sure how to update to 11 - or if I even need to).
PHP version: 5.3.28 (not sure where to look in phpinfo for driver information).
Not sure how to check that MSSQL Native Client is installed.
I'm unable to find my php.ini file. It's supposedly located in C:\Program Files (x86)\PHP\v5.3\php.ini but I don't see it.
Any help would be much appreciated.

From the error message I assume that the connection works but your credentials don't. Is it really correct to use userName as the username and DBname as the db name. Looks like you have a copy/paste problem there.
Regarding the ini file: You should see it in the phpinfo output, search for Loaded Configuration File.

Related

Database Connection error using sql server and codeigniter in IIS

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/

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 SQLSRV Error using machine account

I am using SQLSRV function using windows authentication but it is using the machine account as a default. How can I change it? I want to run using HOMESBUILD\john.davis
I am running PHP 5.3.10 on WAMP Server 2.2
Error Message:
Connection could not be established.
Array ( [0] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'HOMESBUILD\KKK-454-IAH05$'.
I had to configure the WAMP server from Services to run as different account.

IIS PHP using Windows Authentication unable to connect to SQL Server 2008R2

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.

PHP SQL Server 2005 Standard Edition connection timeout

I've searched through endless solutions for connection problems with PHP and SQL Server 2005 and still no luck. I'm using SQL Server 2005 Standard Edition.
Here's the code:
$server = "MYLAP01\\SQLEXPRESS";
$options = array("Database" => "test1");
$conn = sqlsrv_connect($server, $options);
if ($conn === false) die("<pre>".print_r(sqlsrv_errors(), true));echo "Successfully connected!";
sqlsrv_close($conn);
And the output:
Array
(
[0] => Array
(
[0] => 08001
[SQLSTATE] => 08001
[1] => -1
[code] => -1
[2] => [Microsoft][SQL Server Native Client 11.0]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].
[message] => [Microsoft][SQL Server Native Client 11.0]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].
)
[1] => Array
(
[0] => HYT00
[SQLSTATE] => HYT00
[1] => 0
[code] => 0
[2] => [Microsoft][SQL Server Native Client 11.0]Login timeout expired
[message] => [Microsoft][SQL Server Native Client 11.0]Login timeout expired
)
[2] => Array
(
[0] => 08001
[SQLSTATE] => 08001
[1] => -1
[code] => -1
[2] => [Microsoft][SQL Server Native Client 11.0]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][SQL Server Native Client 11.0]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.
)
)
Fixed this by adding the port number: $server = "MYLAP01\SQLEXPRESS,1433";

Categories