Home >Backend Development >PHP Tutorial >How to Fix the 'Fatal error: Class 'MySQLi' not found' PHP Error?

How to Fix the 'Fatal error: Class 'MySQLi' not found' PHP Error?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-17 12:46:24209browse

How to Fix the

Troubleshooting "Fatal error: Class 'MySQLi' not found" in PHP

When encountering the error "Fatal error: Class 'MySQLi' not found," it indicates that the MySQLi extension is not enabled or configured correctly in your PHP environment. Here's how to resolve this issue:

Checking PHP Version

Ensure that you are running a PHP version that supports MySQLi. Version 5.2.5 is outdated and does not include MySQLi by default. Consider upgrading to a more recent PHP version.

Installing MySQLi Extension

If you are using a compatible PHP version, you need to install the MySQLi extension. You can typically do this:

  • For Linux/Unix:

    yum install php-mysqli

    or

    apt-get install php-mysqli
  • For Windows:

    • If using XAMPP, enable the MySQLi extension in the XAMPP control panel.
    • Otherwise, follow the instructions for installing PHP extensions on your specific Windows operating system.

Enabling MySQLi Extension

After installing MySQLi, you must ensure that it is enabled in your php.ini configuration file. Add the following line to your php.ini file:

extension=mysqli.so

Restart Web Server

Once you have installed and enabled MySQLi, restart your web server (e.g., Apache or Nginx) to apply the changes.

Verifying Installation

Check your PHPInfo page (e.g., by creating a script with phpinfo();) and look for the "mysqlnd" section. This section should list MySQLi as one of the available extensions.

Sample Code

Here's a modified version of your sample code, assuming MySQLi is now installed and enabled:

<?php

$mysqli = new mysqli($db_server, $db_user, $db_pass, $db_name);

if ($mysqli->connect_errno) {
  echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

The above is the detailed content of How to Fix the 'Fatal error: Class 'MySQLi' not found' PHP Error?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn