Database Connection error using sql server and codeigniter in IIS - php

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/

Related

Unable to connect SQL Server from PHP using Windows Authentication

I'm trying to connect to MS-SQL Server from PHP-8.0 (hosting on IIS-10 localhost) using SQL Server Driver for PHP, i.e., SQLSRV-5.9. While connecting from a PHP page, if I use SQL Server Authentication, it's getting connected right away. But while using Windows Authentication, it's throwing an error in the browser saying:
Connection could not be established.
Array ( [0] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456
[code] => 18456 [2] => [Microsoft][ODBC Driver 17 for SQL Server][SQL
Server]Login failed for user 'NT AUTHORITY\IUSR'. [message] =>
[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for
user 'NT AUTHORITY\IUSR'. ) [1] => Array ( [0] => 28000 [SQLSTATE] =>
28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 17
for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'.
[message] => [Microsoft][ODBC Driver 17 for SQL Server][SQL
Server]Login failed for user 'NT AUTHORITY\IUSR'. ) )
My environment is Windows 10 Pro, Version 21H2, 64-bit. In my SQL Server, both Windows Authentication and SQL Server Authentication are enabled, and I'm able to connect to the server using both of them through SQL Server Management Studio. So what's stopping my PHP code to connect to the server using Windows Authentication? Both the PHP and SQL Sever are on the same PC. What am I doing wrong? As I'm new in PHP, can you please clarify it with a simple example?
My connect-db.php code follows. Note how I used connection string for both Windows and SQL Server Authentication.
?php
$serverName = "(local)"; // Optionally use port number (1433 by default).
$connectionString = array("Database"=>"TestDB"); // Windows Authentication connection string.
// $connectionString = array("UID"=>"sa","PWD"=>"sa","Database"=>"TestDB"); // SQL Server Authentication connection string.
$conn = sqlsrv_connect($serverName, $connectionString); // Connect to the server.
if($conn === false) {
echo "Connection could not be established.<br/>";
die(print_r(sqlsrv_errors(), true));
} else {
echo "Connection established successfuly.<br/>";
}
sqlsrv_close($conn); // Close connection resources.
?>
Thanks and Regards!

Problem with sqlsrv_connect on php, linux server

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

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));
}
?>

On Joomla set up cannot connect to MSSQL due to user name error [duplicate]

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

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.

Categories