Reasons for MySQLi Class Not Found

Why you might be getting an error stating that mysqli is not found, even when it is installed.

396 views
d

By. Jacob

Edited: 2020-05-17 10:32

I recently had to deal with a Class 'mysqli' not found problem on a server I am maintaining. There may be multiple reasons why this error is occurring, so you should read this carefully. Also note that this may not be a complete guide, but it should give you an idea as to what is happening, and how to fix it.

When configuring servers it is extremely important that we know the "standard" way of doing things in our Linux distribution, as well as the location of various configuration files. You can consult the help files of your distribution to learn the location of the files you need.

Note. This guide should work for both Debian and Ubuntu, and maybe others as well.

First, you should make sure that the mysql PHP extension is installed. You can do this in several ways. But, it does no harm to simply try and install it regardless. Run the following command:

sudo apt install php7.4-mysql

Note. You need to install the package that corresponds with your PHP version. If you do not know which package you need, you can try to search for it using apt-cache search php, or you can easily predict it. Here is a few examples:

  1. php7.3-mysql
  2. php7.2-mysql
  3. php5-mysql

Also, if you are working in a namespace, you will need to add a backslash (\) in front of the mysqli class when instantiating a new database object:

// Enable errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL); // Connect to database
$db = new \mysqli($host, $user, $password, $database);

If something is not working, you should see an error in your browser window.

PHP-FPM Configuration

If MySQL is, in fact, installed, and if it also shows up when running a file with phpinfo();, then it may be because you have loaded the wrong PHP-FPM configuration.

This typically happens after updating your server, as the old configuration file might still be active, for whatever reason, even after installing the latest PHP-FPM version.

To fix it, you need to disable the old one and enable the new one. You can find out which version is enabled easily if using Apache, simply:

ls /etc/apache2/conf-enabled

Then disable the one that you got enabled, and enable the new one:

sudo a2disconf php7.3-fpm
sudo a2enconf php7.4-fpm

In case you need it, you can also get a list of available configuration files:

ls /etc/apache2/conf-available

Do not forget to restart or reload Apache after messing around with your configuration files.

sudo service apache2 restart

Tell us what you think:

  1. How to configure phpMyAdmin with automatic login by setting auth_type to config.
  2. How to create new users in MySQL and control their permissions for better security.
  3. How to generate sitemaps dynamically using PHP.
  4. How to perform simple SELECT statements in SQL to communicate with SQL databases.
  5. The error happens when importing database backups using the SOURCE command, either because you got the path wrong, or because you used the command incorrectly.

More in: MySQL