Error 08001 while connecting to SQL Server by using PHP - php

Here is my problem, I have my connection handles like this:
$dbSrvName = 'servername';
$dbName = 'database';
$dbUser = 'user';
$dbPass = 'password';
$dbInfo = array(
'Database' => $dbName,
'UID' => $dbUser,
'PWD' => $dbPass
);
$this->dbConn = sqlsrv_connect($dbSrvName, $dbInfo);
if(!$this->dbConn)
{
die(print_r( sqlsrv_errors(), true), 1);
}
When I trigger output from sqlsrv_errors() I get the following message:
Array ( [0] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 26
[code] => 26 [2] => [Microsoft][SQL Server Native Client 10.0]Client
unable to establish connection because an error was encountered during
handshakes before login. Common causes include client attempting to
connect to an unsupported version of SQL Server, server too busy to
accept new connections or a resource limitation (memory or maximum
allowed connections) on the server. [message] => [Microsoft][SQL
Server Native Client 10.0]Client unable to establish connection
because an error was encountered during handshakes before login.
Common causes include client attempting to connect to an unsupported
version of SQL Server, server too busy to accept new connections or a
resource limitation (memory or maximum allowed connections) on the
server. ) [1] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 10054
[code] => 10054 [2] => [Microsoft][SQL Server Native Client 10.0]TCP
Provider: An existing connection was forcibly closed by the remote
host. [message] => [Microsoft][SQL Server Native Client 10.0]TCP
Provider: An existing connection was forcibly closed by the remote
host. ) [2] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 26
[code] => 26 [2] => [Microsoft][SQL Server Native Client 10.0]Client
unable to establish connection [message] => [Microsoft][SQL Server
Native Client 10.0]Client unable to establish connection ) [3] =>
Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 10054 [code] => 10054
[2] => [Microsoft][SQL Server Native Client 10.0]Client unable to
establish connection due to prelogin failure [message] =>
[Microsoft][SQL Server Native Client 10.0]Client unable to establish
connection due to prelogin failure ) ) [2014-12-31 08:35:25]
SQLSRV USED:
php_sqlsrv_53_ts_vc9.dll
So I am afraid the version of SQL is not compatible with this version of sqlsrv. Any ideas?
Thank you in advance and Happy New Year!

I am going to guess you are trying to remotely connect to the sql server. You need to make sure that is actually possible with your sql server configuration.
Then you need to make sure that the user account you are trying to connect with has proper permissions
Then firewall needs to be opened as well on the server...
here is a short list of things to check.
Can't Connect Remotely

Actually, it was a misunderstanding! The database that I was trying to connect to is Oracle. So the PHP library is not compatible.
Thanks anyway...

Related

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

Unable to connect to my mssql database using 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.

Can't Establish connection to MS SQL Server 2008

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.

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";

PHP connecting to SQL Server on localhost returns Error Locating Server/Instance Specified

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.

Categories