Fatal error: Call to undefined function sqlsrv_connect()
When your PHP script throws a Fatal error: Call to undefined function sqlsrv_connect() immediately upon execution, it is almost never a logic error in your connection string or database configuration. It indicates that the PHP runtime cannot locate the compiled extension responsible for interacting with SQL Server. This is a classic deployment and environment configuration issue, specifically regarding how WAMP setups handle mixed-database architectures.
The source material highlights a common misconception: the belief that loading an extension requires only adding the extension=... line to php.ini. While this syntax is correct, the mechanism for locating the .dll file is often misconfigured in local development environments like WAMP. The error persists because the PHP interpreter searches for extensions relative to its own installation directory (ext folder), whereas users frequently copy DLLs into the root of the project or assume the bin/php.ini path automatically resolves to the correct binary location without verifying the absolute path matches the running PHP version.
Verifying the Extension Load Order
The most reliable way to confirm whether an extension is loaded is not by checking the file system, but by inspecting the runtime state via phpinfo(). If you do not see a section labeled "SQLSRV" or "sqlsrv" when running <?php phpinfo(); ?>, the extension is simply not active.
This failure often stems from the specific PHP version mismatch between the installed DLL and the configured php.ini. In the provided source material, the user configures extension=c:/wamp/bin/php/php5.4.16/ext/php_sqlsrv_53_ts.dll but notes later that they switch to php_sqlsrv_54_ts.dll. This is a critical detail. The suffix in the filename dictates compatibility:
_53_: Compatible with PHP 5.3.x_54_: Compatible with PHP 5.4.x
If your WAMP stack is running PHP 5.4 but you load a DLL intended for PHP 5.3, the extension loader will ignore the file silently (or throw an error depending on settings), resulting in the function being undefined. You must ensure the filename suffix matches the major version number of your php.ini exactly.
The WAMP Multi-Database Confusion
A significant portion of the troubleshooting effort in similar cases revolves around the architecture of the local server. WAMP (Windows, Apache, MySQL) is pre-configured to manage MySQL by default. However, SQL Server is a distinct product with its own service manager (wampsql or standalone sqlservr.exe).
The error does not occur because you are trying to connect to SQL Server from a MySQL host; PHP has no inherent restriction on which database engine it talks to. The issue arises if the WAMP environment is misconfigured to expect MySQL connections while attempting to load Microsoft-specific extensions, or if the wampsql service is failing silently due to permission issues, preventing the driver from initializing properly.
To resolve this, you must ensure that:
- Service Status: The SQL Server service (often labeled
SQL Expressin WAMP) is actually running and accessible via Windows Services (services.msc). - Driver Compatibility: You are using a driver version compatible with your specific PHP build (TS vs NT). The source material references
_ts.dll, indicating a Thread-Safe build, which is required if Apache runs as a separate process from IIS or if the PHP binary was compiled with thread-safety enabled (standard for production and WAMP).
Correct Configuration Steps
To fix the Call to undefined function error without reinventing the wheel:
- Locate the Active PHP Binary: Run
where php.exein your terminal. This often reveals multiple paths. Ensure yourphp.iniis located in the folder returned by that command, not just anybin/php.ini. - Match the DLL Version: If you have PHP 5.4 installed, look for
php_sqlsrv_54_ts.dll. Do not usephp_sqlsrv_53_ts.dll. - Verify Paths in php.ini: Open the active
php.iniand uncomment or add the line using an absolute path:
Note the quotes around the path; they are required if the path contains spaces (though WAMP paths usually do not).extension="C:/wamp/bin/php/php5.4.16/ext/php_sqlsrv_54_ts.dll" - Restart Apache: Changes to
php.iniare not picked up until the web server restarts. In WAMP, this is simply clicking the red/green icon and selecting "Restart All Services". - Confirm Loading: Visit your local site and run
<?php phpinfo(); ?>. Scroll down to the bottom or search for "SQLSRV". If the section appears, the function is available.
If you still encounter connection timeouts after confirming the extension is loaded, the issue shifts from PHP configuration to network latency or SQL Server authentication mode (Windows Authentication vs. Mixed Mode). However, the undefined function error specifically points to the missing DLL as the root cause. For those managing complex Laravel applications on Windows, ensuring these extensions are loaded correctly is often a prerequisite before attempting any database migrations.