0

I'm trying use PHP to establish a connection to a SQL server

I can access the server remotely using SQL Server Management studio.

Then if i run:

select @@servername + '\' + @@servicename

I get:

Server_Name\Instance_Name

(maybe it's worth noting that i can't use this Server_Name to connect to the server in Management Studio - I have to use the public IP instead, not sure why, but i am noob so maybe that's normal?)

The PHP script i am running (On the same machine i am using to establish the Management Studio Connection) looks like this:

<?php

$username = "same_user_as_for_management_studio";

$password = "same_pass_as_for_management_studio";

$serverName = "Server_Name\Instance_Name";

$db = "Example";


$connectionInfo = array( "Database"=> $db, "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));
}
?>

I have tried substituting the server name for the public IP but still no joy. The error i get is:

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. ) )
teebagz
  • 656
  • 1
  • 4
  • 26
  • 1
    syntax error here `$password = "same_pass_as_for_management_studio"';` for one thing. It's throwing off syntax highlighting. – Funk Forty Niner Oct 09 '15 at 15:20
  • @Fred-ii- that happened when i changed the variable name for the purpose of posting and isn't present in the actual code, but thank you – teebagz Oct 09 '15 at 15:22
  • what you mean exactly with "can't establish a connection"? do you get any error message? – Paolo Oct 09 '15 at 15:22
  • @Paolo read the error in the question – teebagz Oct 09 '15 at 15:23
  • 3
    er... lazy reading -.- – Paolo Oct 09 '15 at 15:24
  • kind of thought so; as I said, it was throwing off syntax highlighting ;-) – Funk Forty Niner Oct 09 '15 at 15:24
  • 1
    For gods sake make that error readable man .. ugh .. with newlines and everything ... btw -> is http://stackoverflow.com/questions/28659796/sql-server-network-interfaces-connection-string-is-not-valid-87 in anyway related ? – DannyZB Oct 09 '15 at 15:25
  • @DanielBatkilin Thank Youuuu!! connection established with no instance name (using the IP address as the server name) p.s. next time i'll try harder with the error message. – teebagz Oct 09 '15 at 15:30
  • 1
    You're welcome (: I flagged this as a duplicate then – DannyZB Oct 09 '15 at 15:32

1 Answers1

0

Is the SQL server running on the default port?

$serverName = "serverName\sqlexpress, 1542"; //serverName\instanceName, portNumber (default is 1433)
Andrew Coder
  • 1,058
  • 7
  • 13