Current Location: Home> Latest Articles> Complete Guide to Fix "Call to undefined function mysqli_connect()" in PHP

Complete Guide to Fix "Call to undefined function mysqli_connect()" in PHP

M66 2025-06-22

How to Fix PHP Error: Call to undefined function mysqli_connect()

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.

Solution 1: Check PHP Configuration

The first step is to make sure the mysqli extension is enabled in your PHP environment. Here's how:

  1. Open your php.ini configuration file, usually located in the conf or root directory of your PHP installation.

  2. Search for the following line:

<span class="fun">;extension=mysqli</span>
  1. If there’s a semicolon ; at the beginning, it means the extension is commented out. Modify it to:

<span class="fun">extension=mysqli</span>
  1. Save the file and restart your web server (e.g., Apache, Nginx).

  2. Run your PHP script again to see if the error has been resolved.

Solution 2: Install and Enable the mysqli Extension

If enabling the extension doesn’t work, your PHP environment might not include the mysqli extension at all. Follow these steps to install it:

  1. Open php.ini and confirm the following settings are enabled (no semicolons):

extension_dir = "ext"
extension=mysqli
  1. Download and extract the correct mysqli extension for your PHP version.

  2. Copy the extension files into the ext folder inside your PHP installation directory.

  3. Save the configuration and restart your web server.

  4. Test your PHP script again to verify the connection is now working.

Solution 3: Check PHP and MySQL Version Compatibility

In some cases, an incompatibility between PHP and MySQL versions can trigger this issue. Here's how to verify compatibility:

  1. Check your current PHP and MySQL versions.

  2. Visit the official PHP documentation to ensure the versions you're using are compatible.

  3. If they are not, consider upgrading PHP or downgrading MySQL to a compatible version.

Solution 4: Verify That mysqli Is Properly Installed

If none of the previous methods work, you need to ensure that the mysqli extension is properly installed:

  1. In php.ini, confirm the extension line exists and is uncommented:

<span class="fun">extension=mysqli</span>
  1. 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>
  1. 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.

Conclusion

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.