Fatal error: Call to undefined function sqlsrv_connect() in C:\xampp\htdocs
When you encounter a Fatal error: Call to undefined function sqlsrv_connect() while trying to connect to Microsoft Azure SQL Server from a local XAMPP environment, the issue usually isn't your application logic. It is almost certainly a configuration mismatch between the PHP version bundled with XAMPP and the Microsoft ODBC Driver or PHP extensions you installed.
In the scenario described, the stack consists of XAMPP 3.2.1 running PHP 5.6.8, attempting to connect to jy4nij6vuy.database.windows.net. The error indicates that the PHP runtime cannot find the sqlsrv function at all. Despite adding php_sqlsrv_56.ts.dll and php_pdo_sqlsrv_56.ts.dll to the extensions folder and uncommenting the corresponding lines in php.ini, the connection fails. This specific behavior points to a fundamental misunderstanding of how XAMPP manages its PHP installations.
The XAMPP Architecture Trap
XAMPP is a distribution that bundles Apache, MySQL (or MariaDB), Perl, and PHP into a single directory structure (C:\xampp). Crucially, it does not use a single php.ini file for the entire system. Instead, PHP is installed as a module within the Apache directory tree, meaning the configuration file you need to edit is located at:
C:\xampp\php\php.ini
If you are following standard installation guides online that point to a generic path like C:\Windows\php.ini or C:\xampp\apache\bin\php.ini, those edits will have zero effect on the Apache server running your local site. You must ensure the extension=php_sqlsrv_56.ts.dll line is uncommented specifically in the file inside the XAMPP PHP directory.
Furthermore, simply copying the DLL files (php_sqlsrv_56.ts.dll) to the extensions folder is not enough if the architecture mismatch exists. The ts suffix in the filename stands for Thread Safe. The dl suffix stands for Non-Thread Safe (used typically by PHP CLI or FPM). Since XAMPP runs Apache, which is a multi-threaded server, you generally need the Thread Safe version. However, if your PHP build was configured differently than the DLLs you downloaded, they will simply fail to load silently until you verify via phpinfo().
Verifying the Extension Load
To confirm whether PHP has actually loaded the extension, you cannot rely on the code in your .php file alone. You must generate a report. Create a new file named test.php with the following content:
<?php phpinfo(); ?>
Open this file in your browser at http://localhost/test.php. Search the page for "sqlsrv". If you see a section titled "SQLSRV" or "PDO_SQLSRV", the extension is loaded. If the section is missing, the configuration line in php.ini is either commented out, the filename does not match exactly (including case sensitivity on Windows), or the DLL is incompatible with PHP 5.6.8.
Azure Connectivity Specifics
Once the extension loads successfully, you must address the connection string format used in your code:
$serverName = "jy4nij6vuy.database.windows.net,1433";
While this format works for local SQL Server instances, connecting to Azure SQL Database from a client application often requires specific authentication modes. The default sqlsrv driver expects the server name to be just the hostname without the port unless you are using a specific instance of SQL Express or an on-premise setup. For Azure SQL Database, the standard connection string is typically:
$serverName = "jy4nij6vuy.database.windows.net";
The port 1433 is implicit in the Azure endpoint and does not need to be appended explicitly unless you are routing through a custom gateway. Additionally, ensure your connection options include proper error handling. The code snippet provided uses die() to stop execution immediately upon failure. In a production environment, or even during local debugging, it is better to log the error using error_log() before terminating the script. This allows you to inspect the detailed SQLSRV error array returned by sqlsrv_errors() without blocking the user interface entirely.
Moving Forward with Modern PHP Versions
While the solution above resolves the immediate error for PHP 5.6, relying on such an ancient version of PHP is a significant technical debt. PHP 5.6 reached End of Life (EOL) in December 2018. This means you no longer receive security updates, and many modern PHP extensions have dropped support for it.
If your project requires robust security patches and access to the latest Microsoft drivers, migrating to a newer PHP version is highly recommended. XAMPP still supports modern versions like PHP 8.x. Upgrading will allow you to use the php_sqlsrv.dll (without the specific version suffix) and potentially switch to PDO SQLSRV, which offers a more consistent API for database interactions compared to the legacy sqlsrv extension.
If you are building a Laravel application, consider leveraging the official Laravel packages for database connections, which abstract away much of this low-level configuration complexity. For more details on configuring secure and efficient database connections in modern PHP stacks, resources like https://laravelcompany.com offer guidance on best practices for handling data persistence.
Ultimately, the fix involves locating the correct php.ini within the XAMPP installation, ensuring the architecture of the DLL matches the PHP binary (Thread Safe), and verifying the load via phpinfo(). Once these foundational steps are complete, your application will be able to execute SELECT, INSERT, UPDATE, and DELETE operations against your Azure SQL server reliably.
