Resolving PHP 7.2 SQLSRV Extension Mismatch on Windows IIS
Attempting to bridge PHP 7.2 with Microsoft SQL Server via the sqlsrv extension often leads to immediate frustration when the connection fails silently. The scenario you are facing is a classic case of version incompatibility between the PHP runtime and the compiled DLL extensions available for that specific release.
The core issue lies in the configuration file, php.ini. In your setup, you have added extension=php_sqlsrv_71_nts_x86.dll to load the extension, yet the connection string fails. This filename explicitly indicates that the binary was compiled against PHP 7.1 (71). When executed by a PHP 7.2 interpreter, the dynamic linker cannot resolve the necessary function signatures or internal structures because the API surface changed between these minor versions. Even though you confirmed the file exists in C:\Program Files (x86)\PHP\v7.2\ext, the naming convention reveals that it belongs to the wrong PHP version entirely.
To resolve this, you must locate the extension DLL specifically built for PHP 7.2. The Microsoft SQL Server driver repository typically hosts separate packages for each major and minor PHP release. You should search your installation directory, C:\Program Files (x86)\PHP\v7.2\ext, for a file named php_sqlsrv_72_nts_x86.dll. If the NTS (Non-Thread Safe) version is not present, you may need to acquire the Thread Safe (TS) variant if your IIS PHP-FPM configuration requires it.
Once the correct binary is identifiedâensuring the filename matches php_sqlsrv_72 instead of php_sqlsrv_71âupdate your php.ini file accordingly:
extension=php_sqlsrv_72_nts_x86.dll
Beyond simply fixing the DLL mismatch, there is a subtle but critical security consideration in your connection logic. Your current code uses standard key-value pairs like UID, PWD, and Database. While this works for simple scenarios, it does not leverage the modern features of the SQLSRV driver, specifically the ability to handle encryption and trust settings robustly.
If you are connecting to a server over HTTPS or if your corporate environment mandates TLS 1.2, you must explicitly enable these options in your $connectionInfo array. Without them, the connection might succeed initially but fail under strict security policies later on. A more resilient configuration looks like this:
$serverName = $_SESSION["server"];
$databaseName = $_SESSION["database"];
$username = $_SESSION["username"];
$password = $_SESSION["password"];
// Ensure encryption is handled explicitly if required by your infrastructure
$connectionInfo = [
"Database" => $databaseName,
"Uid" => $username,
"PWD" => $password,
"Encrypt" => true, // Forces encryption, critical for many modern environments
"TrustServerCertificate" => false // Set to true only if you cannot validate the cert chain
];
$conn = sqlsrv_connect($serverName, $connectionInfo);
Note that sqlsrv_connect returns false on failure rather than throwing an exception. This means your application will not automatically crash upon connection errors unless you wrap the logic in a try-catch block. However, checking the return value is mandatory:
if ($conn === false) {
$errors = sqlsrv_errors();
// Handle specific error messages here before logging or displaying to user
}
Another potential pitfall involves the session handling in your script. You are storing raw credentials in $_SESSION. While this keeps the logic within the PHP file, ensure that your session storage is secure and that these sensitive values are never serialized into logs or error reports. Ideally, pass these parameters directly via POST to the query function without persisting them in the session array for long-term storage.
Finally, remember that sqlsrv is a native extension requiring compiled binaries. If you ever switch PHP versions (e.g., moving from 7.2 to 8.x), you cannot reuse the same DLLs; the entire driver package must be re-downloaded and configured for the new runtime version.
