W3docs

Fatal error: Call to undefined function mysqli_connect()

The "Fatal error: Call to undefined function mysqli_connect()" error in PHP typically occurs when the MySQLi extension is not enabled or properly configured in your PHP environment.

Introduction:

The "Fatal error: Call to undefined function mysqli_connect()" error in PHP typically occurs when the MySQLi extension is not enabled or properly configured in your PHP environment. To solve this issue, you can follow the steps below:

Some steps to take into consideration:

1- Check if MySQLi extension is enabled: Make sure that MySQLi extension is enabled in your PHP environment. You can check this by looking for "mysqli" in the list of enabled extensions in your PHP configuration. You can create a PHP file with the following code to check the enabled extensions:

Checking if MySQLi extension is enabled

<?php
phpinfo();
?>

Access this file in your web browser and search for "mysqli" to see if it's listed. If not, you may need to enable the MySQLi extension in your PHP configuration.

2- Install MySQLi extension: If MySQLi extension is not installed on your server, you may need to install it. The process depends on your server environment. For example, on Ubuntu/Debian with a LAMP stack, install the package using:

sudo apt-get install php-mysql

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Learn object oriented PHP</div>

After installing, ensure the extension is enabled in your php.ini file. Locate the line ;extension=mysqli and remove the leading semicolon to uncomment it. Save the file and restart your web server to apply the changes. (For Windows, ensure the same line is uncommented in your php.ini. For Docker, rebuild your image with the php-mysql package.)

3- Check PHP version: Make sure that you are using a PHP version that supports MySQLi extension. MySQLi extension requires PHP version 5.3.0 or higher. You can check your PHP version by creating a PHP file with the following code:

Checking PHP version

<?php
echo phpversion();
?>

Access this file in your web browser to see your PHP version. If you are using an older version of PHP, you may need to upgrade to a version that supports MySQLi extension.

4- Correctly configure MySQLi connection: If MySQLi extension is enabled and installed, make sure that you are correctly configuring your MySQLi connection in your PHP code. Here's an example of how to establish a MySQLi connection:

configuring MySQLi connection

<?php
$mysqli = new mysqli("localhost", "username", "password", "database");
if ($mysqli->connect_errno) {
    die("Failed to connect to MySQL: " . $mysqli->connect_error);
}
?>

Replace "username", "password", and "database" with your actual MySQL credentials. Make sure that you are using the correct hostname (usually "localhost" or "127.0.0.1") and that your MySQL server is running and accessible. (Note: This example uses the object-oriented approach. If you prefer the procedural style mentioned in the error, use $mysqli = mysqli_connect("localhost", "username", "password", "database"); instead.)

5- Check for typos or missing code: Double-check your PHP code for any typos or missing code related to MySQLi functions. Make sure that you are using the correct function name ("mysqli_connect()" instead of "mysql_connect()") and that you are properly calling and using MySQLi functions in your code.

6- Restart web server and clear cache: After making changes to your PHP configuration or code, restart your web server to apply the changes. Additionally, clear your browser cache to ensure that you are testing the latest version of your PHP code.

Conclusion:

By following these steps, you should be able to solve the "Fatal error: Call to undefined function mysqli_connect()" error in PHP and successfully establish a MySQLi connection. If you are still encountering the issue after trying these solutions, it's recommended to seek further assistance from a PHP developer or a system administrator.