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
Release: 2024-12-17 12:46:24
Original
202 people have browsed it

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
    Copy after login

    or

    apt-get install php-mysqli
    Copy after login
  • 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
Copy after login

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;
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template