When working with PHP, it's common to use a MySQL database for storing and managing data. The mysqli extension is a widely used method for connecting to MySQL, offering a rich set of functions. However, some developers might encounter the following error while using the mysqli_connect() function:
PHP Fatal error: Call to undefined function mysqli_connect() in file.php on line X
This error means that PHP does not recognize the mysqli_connect() function, usually because the required extension is either not installed or not enabled. This guide outlines several effective ways to resolve this problem.
The first step is to make sure the mysqli extension is enabled in your PHP environment. Here's how:
Open your php.ini configuration file, usually located in the conf or root directory of your PHP installation.
Search for the following line:
<span class="fun">;extension=mysqli</span>
If there’s a semicolon ; at the beginning, it means the extension is commented out. Modify it to:
<span class="fun">extension=mysqli</span>
Save the file and restart your web server (e.g., Apache, Nginx).
Run your PHP script again to see if the error has been resolved.
If enabling the extension doesn’t work, your PHP environment might not include the mysqli extension at all. Follow these steps to install it:
Open php.ini and confirm the following settings are enabled (no semicolons):
extension_dir = "ext"
extension=mysqli
Download and extract the correct mysqli extension for your PHP version.
Copy the extension files into the ext folder inside your PHP installation directory.
Save the configuration and restart your web server.
Test your PHP script again to verify the connection is now working.
In some cases, an incompatibility between PHP and MySQL versions can trigger this issue. Here's how to verify compatibility:
Check your current PHP and MySQL versions.
Visit the official PHP documentation to ensure the versions you're using are compatible.
If they are not, consider upgrading PHP or downgrading MySQL to a compatible version.
If none of the previous methods work, you need to ensure that the mysqli extension is properly installed:
In php.ini, confirm the extension line exists and is uncommented:
<span class="fun">extension=mysqli</span>
You can check loaded PHP extensions using the following commands:
On Linux:
<span class="fun">php -m | grep mysqli</span>
On Windows:
<span class="fun">php.exe -m</span>
If mysqli is missing from the list, the extension was not installed correctly. Reinstall the correct version of the extension based on your PHP environment.
To resolve the Call to undefined function mysqli_connect() error, start by checking your PHP configuration and ensure the mysqli extension is enabled. If necessary, install the correct extension files and verify compatibility between PHP and MySQL. These steps should help you eliminate the error and restore your database connection functionality in PHP.