Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MSSQL connection Noobie

Status
Not open for further replies.

andyc209

IS-IT--Management
Dec 7, 2004
98
GB
Sorry i know this is basic but i am trying to connect to MSSQL - usually use ASP but trying PHP - and getting errors with my connection string.

<?php
$serverName = "IPAddress\server,port";
$connectionInfo = array( "Database"=>"dbName", "UID"=>"****", "PWD"=>"********");
$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));
}
?>

The database name etc is the same i use for ASP so i know it exists

but i get the following error when i launch the PHP page in my browser (sorry its long)

Connection could not be established.
Array ( [0] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 87
Code:
 => 87 [2] => [Microsoft][SQL Server Native Client 11.0]SQL Server Network Interfaces: Connection string is not valid [87]. [message] => [Microsoft][SQL Server Native Client 11.0]SQL Server Network Interfaces: Connection string is not valid [87]. ) [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] => 87 [code] => 87 [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. ) )


Any help is appreciated
 
What portions of the error message have you ruled out? Does the SQL server allow remote connections?

Is the following line completely accurate?

Code:
$serverName = "IPAddress\server,port";

I assumed the format would be IPAddress:port or just server. I'm not sure why one would mix an IP address and a server name.
 
PHP:
$conn = sqlsrv_connect( $serverName, $connectionInfo);

$connectionInfo is an array NOT a string

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
ChrisHirst said:
$connectionInfo is an array NOT a string

That part looks correct and copies what appears in the PHP docs example.


I suspect something is wrong with the definition of $serverName or UID or PWD. Perhaps there is a special character in these strings that needs to be escaped in PHP. Maybe the password has a dollar sign?...(pa\$sw0rd instead of pa$sw0rd).
 
$connectionInfo is an array NOT a string

So?

sqlsrv_connect expects an array. The variable is an array. Seems o.k to me.

It seems the error stems more form a network issue. IE sqlsrv_connect is not resolving the server/instance correctly or is unable to connect to it.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
That part looks correct and copies what appears in the PHP docs example.

Guys, I'm simply correcting the semantics of the terminology between ASP and PHP

In ASP/Vbscript it IS a string in the actual sense of the term in a programming context in PHP it is not.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
The Servername part has to be "Servername\Instancename,Port", for local servers instead of IP 127.0.0.1 or localhost rather use "(local)". If runnning SSQLEXpress the instance name is SQLExpress, the first and main SQL Server instance simply is called SQLServer, and if you didn't used any other port you'r Servername variable might eihter be "(local)\SQLExpress" or "(local)\SQLServer".

This works fine for me, for example:

PHP:
<?php
$serverName = "(local)\SQLServer";
$connectionInfo = array( "Database"=>"test");
$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));
}
?>

Besides that you need the ODBC 11.0 or higher Driver, not Native Client ODBC 11.0 or higher.
[URL unfurl="true"]https://www.microsoft.com/en-us/download/details.aspx?id=36434[/url]

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top