Resolving the Call to undefined function sqlsrv_connect() Error on Windows Server 2008 R2
Encountering a "Call to undefined function sqlsrv_connect()" error immediately after installing the SQLSRV driver is a frustrating experience, particularly when you have verified that the DLL files exist and Apache has restarted. The core issue here is rarely a missing file or a failed service restart; it is almost exclusively a configuration mismatch between the PHP binary architecture and the compiled extension DLLs.
In the scenario described with XAMPP 1.8.2 on Windows Server 2008 R2, the phpinfo() function correctly shows that sqlsrv is not listed, even though the extensions are present in the extension_dir. This discrepancy usually points to one of two specific problems: a mismatch between Thread-Safe (TS) and Non-Thread-Safe (NTS) builds, or an architecture conflict between 32-bit and 64-bit components.
The TS vs. NTS Architecture Mismatch
The most common cause for the sqlsrv_connect function being undefined is loading the wrong extension variant. PHP extensions on Windows are compiled specifically to match the core PHP binary's threading model. You must ensure that the php_sqlsrv.dll matches your php.exe.
If your php.ini contains:
extension=php_pdo_sqlsrv_54_ts.dll
extension=php_sqlsrv_54_ts.dll
The suffix _ts.dll indicates these are Thread-Safe extensions. These should only be used if your main PHP binary is also a Thread-Safe build. If you are running the standard XAMPP distribution, it often defaults to a Non-Thread-Safe (NTS) build, especially on older versions like 1.8.2.
Loading a TS extension into an NTS PHP core results in a silent failure where the function is never registered, leading directly to the Call to undefined function error. To verify this, check your main php.exe properties or use the command line:
php -i | findstr "Thread Safe"
If this outputs "Thread Safe => Off" but your extension files have _ts.dll, you must download and install the corresponding NTS versions (php_pdo_sqlsrv_54_nts.dll and php_sqlsrv_54_nts.dll). Alternatively, switch your PHP core to a TS build.
64-bit vs. 32-bit Architecture Conflicts
Another frequent pitfall involves the system architecture of SQLSRV and SQLNCLI. The source material mentions installing sqlncli (Microsoft SQL Server Native Client). Depending on your PHP environment, you might be running a 32-bit or 64-bit version of PHP.
If your PHP installation is 64-bit but you install a 32-bit version of the SQLSRV driver (or vice versa), the DLL will load into memory but remain unusable because the pointer sizes do not align with the core PHP engine. Given that XAMPP 1.8.2 was released during a transition period in Windows Server architectures, it is highly probable that one component is 32-bit and another is 64-bit.
To resolve this:
- Determine your PHP architecture by running
php -i | findstr "Architecture"or checking the path ofphp.exe. If the path containsx86, you are on a 32-bit system. - Ensure the SQLSRV driver and the Native Client components match that architecture exactly.
- Reinstall the XAMPP stack if necessary to ensure all components (Apache, MySQL/MariaDB, PHP) share the same bit-width architecture.
Verifying the Driver Installation
Once the architecture mismatch is resolved, you must confirm that the extension loads correctly. Simply having the DLL in C:\xampp\php\ext is not enough; it must be explicitly enabled and recognized by the specific PHP binary running Apache.
After ensuring the correct _nts.dll or _ts.dll files are present:
- Restart the XAMPP Control Panel services (Apache and MySQL).
- Create a new file named
info.phpin your web root with the following content:<?php phpinfo(); ?> - Access this file via your browser.
If the sqlsrv section appears, you can proceed with your connection code. If it still does not appear despite having the correct files and enabled extensions in php.ini, check the Apache error logs located typically in C:\xampp\apache\logs\error.log. Look for messages like "PHP Startup: Unable to load dynamic library..." which will explicitly state if a dependency is missing or the architecture is wrong.
Connecting to SQL Server 2005
Regarding the specific connection details, you are attempting to connect from a modern environment (Windows Server 2008 R2) to an older SQL Server 2005 instance. While sqlsrv_connect is generally robust, ensure that the connection string parameters align with the target server's capabilities. The provided code snippet uses standard authentication:
$connectionInfo = array('Database'=>'Eque', "UID"=>"develop", "PWD"=>"develop");
$conn = sqlsrv_connect($serverName, $connectionInfo);
This approach works well for SQL Server 2005, provided the network path is accessible and the driver architecture issues are resolved. If you encounter further connectivity errors after fixing the undefined function issue, check that the firewall allows traffic on port 1433 and that the SQL Server service on the remote host is configured to allow mixed-mode authentication if necessary.
For more advanced database integration patterns or if you are migrating larger applications, exploring dedicated resources for database connectivity best practices can be beneficial.