I have been trying to establish a connection to a SQL Server 2008 but i get a return message saying:
Connection could not be established. Array ( [0] => Array ( [0] =>
08001 [SQLSTATE] => 08001 [1] => -1 [code] => -1 [2] =>
[Microsoft][ODBC Driver 11 for SQL Server]SQL Server Network
Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].
[message] => [Microsoft][ODBC Driver 11 for SQL Server]SQL Server
Network Interfaces: Error Locating Server/Instance Specified
[xFFFFFFFF]. ) [1] => Array ( [0] => HYT00 [SQLSTATE] => HYT00 [1] =>
0 [code] => 0 [2] => [Microsoft][ODBC Driver 11 for SQL Server]Login
timeout expired [message] => [Microsoft][ODBC Driver 11 for SQL
Server]Login timeout expired ) [2] => Array ( [0] => 08001 [SQLSTATE]
=> 08001 [1] => -1 [code] => -1 [2] => [Microsoft][ODBC Driver 11 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 11 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. )
My PHP Code:
<?php
$serverName = "SERV002\SQLEXPRESS"; //serverName\instanceName
$connectionInfo = array( "Database"=>"YSHSDB", "UID"=>"sa", "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));
}
?>
UPDATE: I have fixed this by installing xampp lite onto my server 2003 running the SQL server however, I would still like to be able to access the SQL server if the web services is running on another machine.
The obvious error here is that your SQL server is not running or is not configured to allow remote connections (I think that is the default for SqlExpress)
You will need to open your Sql Connection Manager and change the settings for TCP connections to make them available for remote connections. You may also have a firewall blocking your Sql Connection somewhere. Make sure port 1433 is exposed in any fire wall.
Open your Server Configuration Manager
Server Configuration Manager
Under SQL Server Network Configuration
Click on Protocols for SQLEXPRESS
Rightclick and access Properties of TCP/IP
Select the IP Addresses Tab
Now configure all the TCP Port(s) to the port you are using in your connection string.
Related
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!
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
I wanna to connect with my database which is hosted on Virtual Machine (VB with Win 8.1). Connection attempt tested by Microsoft Data Link on main machine, returns successful connection but my localhost always display a message:
[0] => 28000
[SQLSTATE] => 28000
[1] => 18456
[code] => 18456
[2] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'sa'.
[message] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'sa'.
For user with 'Enforce password policy' enabled I got:
[0] => 28000
[SQLSTATE] => 28000
[1] => 18488
[code] => 18488
[2] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'userDB2'. Reason: The password of the account must be changed.
[message] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'userDB2'. Reason: The password of the account must be changed.
Same code working fine on VM localhost. In both cases, PHP 7.4 is used (with the latest drivers from https://github.com/microsoft/msphpsql).
Server Authentication settings (VM):
Script used:
$server = '127.0.0.1';
$cinfo = array(
"Database"=>'mydbname',
"UID"=>'sa',
"PWD"=>'root'
);
$conn = sqlsrv_connect($server, $cinfo);
if ($conn === false) {
echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
exit;
}
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
Firewall on VM is disabled.
TCP/IP protocol in SQL Server Configuration is enabled.
Port forwarding in VirtualBox settings:
I tried to use different users with different settings but always this errors appears.
UPDATE:
Last SQL Logs
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
EDIT: Sorry, I figured out what the problem was. I was editing the wrong file.
I'm (trying) to use PHP 5.3.5 to connect to my SQL SERVER 2005 but it has been nothing but trouble. On the local machine, if I use SQLCMD -S (local) -U sqlUser -P 1234 -d MyTable it connects fine. But the following code produces an error:
<?php
$myServer = "(local)";
$myUser = "sqlUser";
$myPass = "1234";
$myDB = "MyTable";
$connectionInfo = array("Database"=> $myDB, "UID" => $myUser, "PWD" => $myPass);
//connection to the database
try{
$dbhandle = sqlsrv_connect($myServer, $connectionInfo)
or die(print_r( sqlsrv_errors(), true));
}catch(Exception $e){
echo "<pre>";
print_r($e);
echo "</pre>";
die;
}
?>
How can I get php to connect to my SQL Server?
Full Error:
Array
(
[0] => Array
(
[0] => 08001
[SQLSTATE] => 08001
[1] => -1
[code] => -1
[2] => [Microsoft][SQL Server Native Client 10.0]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].
[message] => [Microsoft][SQL Server Native Client 10.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 10.0]Login timeout expired
[message] => [Microsoft][SQL Server Native Client 10.0]Login timeout expired
)
[2] => Array
(
[0] => 08001
[SQLSTATE] => 08001
[1] => -1
[code] => -1
[2] => [Microsoft][SQL Server Native Client 10.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 10.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.
)
)
If PHP doesn't use the same method to connect as SQLCMD you'll be seeing odd results. Doesn't PHP just use TCPIP to connect (meaning your server should be setup to listen on TCPIP as well).
Stupid mistake, I was editing the wrong file.