×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

MSSQL connection Noobie

MSSQL connection Noobie

MSSQL connection Noobie

(OP)
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

RE: MSSQL connection Noobie

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.

RE: MSSQL connection Noobie

CODE --> 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

RE: MSSQL connection Noobie

Quote (ChrisHirst)

$connectionInfo is an array NOT a string

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

https://secure.php.net/manual/en/function.sqlsrv-c...

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).

RE: MSSQL connection Noobie

Quote:


$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

RE: MSSQL connection Noobie

Quote:

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

RE: MSSQL connection Noobie

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:

CODE --> 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.
https://www.microsoft.com/en-us/download/details.aspx?id=36434

Bye, Olaf.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close