[Solved]: Call to undefined function mysqli_connect()

  John Mwaniki /    Updated on 07 Jul 2024

I have seen it severally where PHP mysqli_connect() database connection worked perfectly well for months or years, then all of a sudden it stops working. On enabling PHP error reporting, or on checking for errors in the error_log file in the cPanel, you find the error below:

PHP Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in /home/username/public_html/...

This can create a great inconvenience and affect your database-driven website negatively especially if you don't visit it regularly.

As you can see from the error above, PHP doesn't recognize the mysqli_connect() function. This usually happens when some changes are made on the server that affects the mysqli extension.

There are 2 ways of connecting PHP to MySQL database:

  • MySQLi extension
  • PDO (PHP Data Objects) extension

Earlier versions of PHP(before version 5) used the MySQL extension which became deprecated in 2012 and was replaced with the MySQLi extension. The "i" in MySQLi stands for improved.

MySQLi extension supports both object-oriented and procedural ways of connecting to the database.

Procedural way

$conn = mysqli_connect("hostname", "username", "password", "database");

Object-Oriented way

$conn = new mysqli("hostname", "username", "password", "database");

All you have to do is to replace the values in quotes above with your real database credentials where the hostname in most cases will be "Localhost", though it may differ in some instances. The "username" is the username of the user assigned to the database, "password" is the password of that user, and "database" is the name of the database you are connecting to.

Either of the above methods will enable communication between PHP and the MySQL database server to take place.

When the error above occurs, the first thing you should do is to check if the PHP MySQL extension module is being loaded.

To do so, simply echo the phpinfo() function. It will show you all the information about your installed PHP version.

<?php
echo phpinfo();
?>

On the PHP information page, scroll down to the section titled "mysqli". You should be able to see the enabled MySQLi library in the Client API library version row as shown below:

phpinfo() mysqli information

There are two libraries for connecting PHP to MySQL database:

  • MySQL native driver for PHP (mysqlnd)
  • MySQL Client Library (libmysql)

The extensions(mysqli or PDO_MySQL) can either use the mysqlnd or libmysql library to connect from PHP to MySQL.

The MySQL Client Library is a general-purpose client library, meaning it can be used across different languages. On the other hand, mysqlnd library is highly optimized for and tightly integrated into PHP. The mysqlnd library is the default library for PHP 5.4 and later versions. MySQL recommends using the MySQL native driver for PHP (mysqlnd) together with ext/mysqli or PDO_MySQL.

In our case from the above screenshot, you can see that mysqlnd is the enabled driver.

We can now scroll more down to the section titled "mysqlnd" for more information as in the screenshot below:

mysqlnd information in phpinfo()

From the last row(API Extensions) of the above table, you can see that both mysqli and pdo_mysql extensions are enabled. This way the database connection works perfectly with no error.

In the case where the database connection results in the undefined function mysqli_connect() error, then you will find that the phpinfo() page doesn't have the "mysqli" and the "mysqlnd" sections.

The solutions to undefined function mysqli_connect() error

1. Upgrading to a later PHP version

In most of the cases that this has happened, I have found that changing PHP to a later version has always worked and fixed the issue. Let's say for example your current PHP version is 7.0, you can change to a later PHP version such as PHP 7.4 or 8.0.

This personally fixed the issue for me the last time it happened to one of the websites I manage.

Here is a brief guide on how to check your website PHP version -> 3 Simple ways of checking your website PHP version

And here is a simple guide for upgrading the PHP version -> How to change the PHP version in cPanel

2. Changing mysqli_connect to new mysqli

Try changing your database connection from procedural to OOP, ie. change replace mysqli_connect to new mysqli. Though it is the OOP way, it doesn't mean that you will have to change anything else in your PHP code. I have seen it severally where making this change fixed the error.

3. Installing the PHP MySQL extension

If you host the website by yourself, you can simply install the php-mysqli extension on Ubuntu by use of the command below:

sudo apt install php-mysqli

You will then be required to restart the apache server with the command below for it to take effect.

sudo service apache2 restart

4. Adding or uncommenting the line "extension=php_mysql.dll" in the php.ini file

If you host the server yourself, open the php.ini file and remove the semicolon in front of the line below:

;extension=php_mysqli.dll

So that it will be like below:

extension=php_mysqli.dll

If this line doesn't exist, simply add it. After making the changes, save and restart your Apache server.

If your website is hosted by a hosting provider/company, and upgrading to a later PHP version or changing mysqli_connect to new mysqli doesn't seem to work for you, simply contact the host with the error message and they will fix it for you.

That's all for this article. It's my hope that it was helpful for you.