How to Use PHP to Connect to SQL Server
If you are attempting to connect PHP to a Microsoft SQL Server instance, you are likely encountering the legacy mssql_* functions or considering the transition to sqlsrv_*. The error message Unable to connect to server: 10.85.80.229 indicates that the TCP/IP handshake failed between your PHP process and the database engine. This is rarely an authentication issue at this stage; it is almost always a network, configuration, or driver mismatch problem.
The Legacy mssql_* Functions Are Deprecated
The code snippet provided uses mssql_connect(). While this function worked in older PHP versions, it relies on the deprecated FreeTDS library. Microsoft has officially deprecated these functions because they lack support for modern SQL Server features and security protocols. If you are running a recent version of PHP (5.6+ or 7.0+), these functions may not even be available unless specifically compiled into the binary.
The modern standard is to use the sqlsrv extension. This extension provides native support for SQL Server, handles newer authentication methods like Windows Integrated Authentication better, and supports features like prepared statements out of the box. Switching from mssql_* to sqlsrv_* is not just a best practice; it is often a requirement for connecting to SQL Server 2012 and later versions without hitting SSL/TLS handshake errors.
Resolving the "Unable to Connect" Error
The specific error Unable to connect to server: 10.85.80.229 means the PHP process cannot establish a TCP connection to that IP address on the default port (usually 1433). Before changing code, you must verify the environment.
First, ensure the SQL Server Browser service is running if you are connecting via name rather than IP, though your example uses an IP directly. More importantly, check the SQL Server configuration manager on the target machine (10.85.80.229). By default, the TCP protocol is often disabled for new instances. You must enable TCP/IP:
- Open SQL Server Configuration Manager.
- Navigate to SQL Server Network Configuration > Protocols for [YourInstance].
- Ensure TCP/IP is set to Enabled.
After enabling it, restart the SQL Server service. This change does not take effect immediately; a restart is required.
Implementing the Modern sqlsrv Connection
Once the network path is verified, update your PHP configuration (php.ini) to enable the sqlsrv extension:
extension=sqlsrv
Ensure this file is located in the directory where PHP loads extensions (often ext/ relative to your php.ini). Restart Apache or CLI after modifying this file.
Your connection code should then look like this:
<?php
$serverName = "10.85.80.229";
$connectionOptions = [
"Database" => "testdb",
"Uid" => "root",
"PWD" => "pass",
"CharacterSet" => "UTF-8"
];
try {
$connInfo = sqlsrv_connect($serverName, $connectionOptions);
if ($connInfo === false) {
die(print_r(sqlsrv_errors(), true));
}
echo "Connected successfully.";
} catch (Exception $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
Notice the use of a try-catch block and sqlsrv_errors(). Unlike the old mssql_* functions which simply return false, the sqlsrv extension throws exceptions or provides detailed error arrays that can be parsed for specific SQL Server errors. If the connection fails, checking the returned array often reveals if the issue is a login failure, a network timeout, or an invalid server name format.
Handling SSL and Trust Issues
If you move to a production environment or use newer PHP versions (7.3+), you may encounter SSL handshake failures even with valid credentials. The mssql_* functions often ignored SSL warnings, but sqlsrv enforces stricter security. You can disable encryption for development environments by adding "TrustServerCertificate" => true to the $connectionOptions array. In a production setting, however, you should configure the server certificate properly rather than disabling encryption.
Integrating with Modern Frameworks
If your project is built on Laravel or similar frameworks, direct usage of raw sqlsrv functions is possible but not recommended for new development. The LaravelCompany approach to database integration typically favors the PDO driver over native extensions where available, as it offers a unified interface for different database systems and better error handling within the framework's ecosystem. However, if you are strictly using raw PHP or a legacy codebase, sticking with sqlsrv is the correct path forward.
Final Verification
If you continue to see connection refused errors after enabling TCP/IP and verifying credentials, check your firewall settings on the SQL Server machine. Port 1433 must be open for the local host connecting out. Additionally, ensure that the SQL Server instance is set to allow remote connections via SQL Server Management Studio, as this setting can sometimes block connections from outside the local subnet depending on network isolation rules.