Problem with sqlsrv_connect on php, linux server - php

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

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

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

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

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