We help IT Professionals succeed at work.
Get Started

PHP MSSQL Connection issues

1,597 Views
Last Modified: 2017-03-07
Hello I am trying to learn how to connect PHP with MSSQL,
However I receive the following error from my script
Could not connect. Array ( [0] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 53 [code] => 53 [2] => [Microsoft][ODBC Driver 11 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. [message] => [Microsoft][ODBC Driver 11 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. ) [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] => 53 [code] => 53 [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. ) )

Open in new window

I have tried the 7 things in the following guide
https://blog.sqlauthority.com/2009/05/21/sql-server-fix-error-provider-named-pipes-provider-error-40-could-not-open-a-connection-to-sql-server-microsoft-sql-server-error/

1.      SQL Server should be up and running. – is running
2.      Enable TCP/IP in SQL Server Configuration – already enabled so I disabled re-enabled and rebooted
3.      Open Port in Windows Firewall – already have a TCP exception for the 1 port 1433
4.      Enable Remote Connection – enabled
5.      Enable SQL Server Browser Service – was stopped, I restarted machine and made sure service was running (no change)
6.      Create exception of sqlbrowser.exe in Firewall – I wasn’t sure to what end this was required, is there another way to do this without just letting anything connect to it.
7.      Recreate Alias- there is no current alias In the machine.

1.      I have also tried specifying the naming instance in the script. And get the following error: could not connect.
 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]

Open in new window

2.      I have also made sure the user and password can be used to login
3.      Please note that this is all for learning purposes this iis not a production server/script.

Here is my script so far:
<html>
<head>
<title>MSSQL STUFF</title>
</head>
<body>
  


<?php  

$serverName = "(servername)";  

/* Get UID and PWD from application-specific files.  */  
$uid = file_get_contents("X:\s\s\uid.txt");  
$pwd = file_get_contents("X:\s\s\pwd.txt");  
$connectionInfo = array( "UID"=>$uid,  
                         "PWD"=>$pwd,  
                         "Database"=>"423");  
						 

$conn = sqlsrv_connect( $serverName, $connectionInfo);  
if( $conn === false )  
{  
     echo "Could not connect.\n";  
     die( print_r( sqlsrv_errors(), true));  
}  

/* Set up the parameterized query. */  
$tsql = "INSERT INTO 423.tss   
        (bur,   
         gur,   
         un,   
         )  
        VALUES   
        (?,?,?,)";  

/* Set parameter values. */  
$params = array(array1,arrt2, $today);  
print "connectionInfo is $connectionInfo";
print "conn is $conn";
print "params is $params";
print "today is $today";
print "stmt is $stmt";
print "tsql is $tsql";

echo "<div id='connectionInfo'>$connectionInfo</div>";
echo "<div id='conn'>$conn</div>";
echo "<div id='params'>$params</div>";
echo "<div id='today'>$today</div>";
echo "<div id='stmt'>$stmt</div>";
echo "<div id='tsql'>$tsql</div>";

/* Prepare and execute the query. */  
$stmt = sqlsrv_query( $conn, $tsql, $params);  
if( $stmt )  
{  
     echo "Row successfully inserted.\n";  
}  
else  
{  
     echo "Row insertion failed.\n";  
     die( print_r( sqlsrv_errors(), true));  
}  

/* Free statement and connection resources. */  
sqlsrv_free_stmt( $stmt);  
sqlsrv_close( $conn);  


?>
</body>
</html>

Open in new window



sorry about all the white space. I made minor edits to the script to protect the integrity of the network/machines in question.
I'm going to suggest it could be something todo with the DLL files on the server, but how do I go about checking what I do and do not need.

I am using server 2012 R2,
SQL server 2008 ( I believe)
PHP 5.6 on IIS
Thank you in advance let me know if you need more info.
Comment
Watch Question
Most Valuable Expert 2011
Author of the Year 2014
Commented:
This problem has been solved!
Unlock 10 Answers and 26 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE