Developing applications in PHP often involves working with databases, with MySQL being one of the most popular choices. To interact with a MySQL database in PHP, the mysqli
extension is used. However, when attempting to use this extension, you might encounter the error: "Fatal error: Uncaught Error: Class 'mysqli' not found". This article delves into a detailed explanation and resolution of this issue.
Causes of the Error
The error "Fatal error: Uncaught Error: Class 'mysqli' not found" typically indicates that the mysqli
extension is not activated in your PHP installation. The mysqli
(MySQL Improved) extension is the recommended way to work with MySQL databases in PHP due to its improvements over the older mysql
interface.
Diagnosing the Problem
Before proceeding with the fix, it's important to verify whether the mysqli
extension is indeed inactive. This can be done by running the following command in your console or terminal:
php -m
This command lists all active PHP extensions. If mysqli
is not in the list, it means the extension is not activated.
Fixing the Error
Activating the mysqli Extension
1. Finding the php.ini File
The first step is to determine which php.ini
configuration file your PHP installation is using. You can find this information by running:
php --ini
2. Editing the php.ini File
Open the identified php.ini
file in a text editor. Look for a line containing ;extension=mysqli
or ;extension=mysqli.so
(for Linux/Mac) and remove the semicolon (;) from it, which deactivates the comment. If this line is missing from your php.ini
file, manually add it to the extensions section.
3. Restarting the Web Server
After saving the changes in the php.ini
file, it's necessary to restart the web server for the changes to take effect. The restart process varies depending on the web server used (Apache, Nginx, XAMPP, WAMP, etc.).
Alternative Solutions
If the problem persists even after activating the mysqli
extension, it's possible that your version of PHP is not compatible with the installed version of the MySQL server, or PHP is not properly installed. In such cases, we recommend:
- Checking the compatibility of PHP and MySQL versions.
- Reinstalling PHP with support for MySQL.
The error "Fatal error: Uncaught Error: Class 'mysqli' not found" is usually caused by the mysqli
extension being inactive in PHP. Fixing this error involves activating the extension through modifications to the php.ini
configuration file and subsequently restarting the web server. If the problem continues, checking the compatibility of PHP and MySQL versions and possibly reinstalling PHP is advisable.