In this blog, we will explore what is this error and why it happens. Moreover, we will also see how we can fix the same.
Table of Content
Error: Call to undefined function sqlsrv_connect()
If you’re trying to connect your PHP application to a SQL Server database, you might see the Fatal error: “Call to undefined function sqlsrv_connect()“.
This failure arises because PHP does not detect the sqlsrv_connect() function needed to connect to SQL Server. The error occurs because you currently lack this driver package or have disabled it.
Why Does This Happen?
- Missing Driver: Your PHP system lacks the driver needed to connect to SQL Server.
- Driver Not Enabled: Your PHP settings did not activate the driver that came with the installation.
- Compatibility Issues: The PHP version you use cannot run with the SQL Server driver installation you have now.
How to Fix It?
1. Install the SQLSRV Driver
The first step requires you to install the right driver for your SQL Server settings. Download your needed SQLSRV driver from Microsoft directly at their website.
2. Enable the Driver in PHP
Once you install the driver PHP needs to recognize its existence. Find your php.ini file (usually in your PHP folder) and make sure these lines are there and not commented out:
extension=php_sqlsrv.dll
extension=php_pdo_sqlsrv.dll
After editing your configuration restart your web server.
3. Check if It’s Working
To check if the driver functions you need to create a basic phpinfo() page. Just create a file with the following code and open it in your browser:
<?php
phpinfo();
?>
After that look for SQLSRV and confirm if the driver is active.
4. Make Sure Versions Match
Check that your SQLSRV driver matches the PHP version you use. If they don’t match, you may run into problems.
This will resolve your error.
Conclusion
The error “Fatal error: This error occurs because PHP cannot locate the appropriate driver to communicate with SQL Server. Setting up SQLSRV and enabling it in your PHP tools lets your PHP app connect to SQL Server smoothly. To learn more about all Fatal errors and how to fix them, check Intellipaat’s SQL course.