128

I am doing a tutorial and am getting this error:

Fatal error: Class 'MySQLi' not found (LONG URL) on line 8

The code on line 8 is:

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

I saw online someone said to see if it was turned on in my phpinfo(), but there wasn't anything listed in there under for "mysqli".

Also, I am running PHP version 5.2.5

Dharman
  • 30,962
  • 25
  • 85
  • 135
Tylor
  • 1,363
  • 2
  • 9
  • 12
  • related: http://stackoverflow.com/questions/19305919/php-mysqli-works-in-command-line-but-not-on-pages – john-jones Aug 03 '15 at 10:44
  • You can use [a handwritten MYSQLi class](http://codecanyon.net/item/php-mysqli-to-mysql-converter-class/2826314), so you don't need to install mysqli. It's the only solution if you don't own the server. – chickens Aug 17 '12 at 12:33
  • if you come from php storm -> https://intellij-support.jetbrains.com/hc/en-us/community/posts/207033955-mysqli-connect-mysql-connect-error- or https://web.archive.org/web/20200805054537/https://intellij-support.jetbrains.com/hc/en-us/community/posts/207033955-mysqli-connect-mysql-connect-error- – Soner from The Ottoman Empire Nov 01 '20 at 10:38
  • On PHP 7.4 at least the class name is lowercase `new mysqli()` – undefined Jul 01 '21 at 22:14

26 Answers26

109

Sounds like you just need to install MySQLi.

If you think you've done that and still have a problem, please post your operating system and anything else that might help diagnose it further.

kalehmann
  • 4,821
  • 6
  • 26
  • 36
Greg
  • 316,276
  • 54
  • 369
  • 333
  • 26
    On AWS, you just run [`sudo yum install php-mysqli`](http://stackoverflow.com/a/23713997/111307) – bobobobo Sep 11 '14 at 16:21
  • For me it was fixed with: apt-get install php7.0-mysql. I am running on Raspbian Stretch 9 – JeanCarlos Chavarria Apr 30 '18 at 15:59
  • On AWS with amazon linux, it would be something like `sudo yum install php56-mysqlnd.x86_64` (pick the one matching to which version of php you have installed) – auspicious99 Dec 03 '18 at 10:46
  • 4
    I am using windows 10 and installed php using a zip folder. my php.int contains extension=mysqli . but I am still getting error undefined function mysqli_connect() – Ali Shan Oct 14 '21 at 08:27
  • On Ubuntu, it’s not part of the distribution anymore. You can install it at any time with:  sudo apt-get install php-mysqlnd – Gary Czychi Feb 20 '23 at 04:01
51

You can check if the mysqli libraries are present by executing this code:

if (!function_exists('mysqli_init') && !extension_loaded('mysqli')) {
    echo 'We don\'t have mysqli!!!';
} else {
    echo 'Phew we have it!';
}
karim79
  • 339,989
  • 67
  • 413
  • 406
47

If you are on Ubuntu, run:

 sudo apt-get install php-mysqlnd

And don't forget to restart the php service after this (Apache or php-fpm depends on the setup).

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
anshuman
  • 3,496
  • 1
  • 19
  • 13
  • 7
    And don't forget to restart Apache after this! – fracz Mar 22 '15 at 23:27
  • @fracz Restarting Apache is the part I forgot. I only restarted MySQL. Thanks. – James May 17 '15 at 16:59
  • 1
    This is not enough. I was still lacking php5-mysql package. Use `sudo` `apt-get -y install php5-mysql php5-mysqlnd` to get both. Is it what you were missing, @Hermann Ingjaldsson ? – Balmipour Sep 17 '15 at 00:33
  • it was some configuration error. http://stackoverflow.com/questions/31535173/enable-mysqli-in-my-in-webserver-copy-of-php – john-jones Sep 17 '15 at 07:22
  • solved my problem, apt install mysqlnd however, my php apps was working before... and it stops with the error above. – Stephane Marchand Jul 29 '21 at 13:03
  • what is the diff between `php-mysql` and `php-mysqlnd`? Why do you need both? – Timo Mar 15 '22 at 20:31
35

If you're calling "new mysqli(..)" from within a class that is namespaced, you might see a similar error Fatal error: Class 'foo\bar\mysqli' not found in. The way to fix this is to explicitly set it to the root namespace with a preceding backslash like so:

<?php 
$mysqli = new \MySQLi($db_server, $db_user, $db_pass, $db_name);
Dharman
  • 30,962
  • 25
  • 85
  • 135
alexkb
  • 3,216
  • 2
  • 30
  • 30
17

In addition to uncommenting the php_mysqli.dll extension in php.ini, also uncomment the extension_dir directive in php.ini and specify your location:

extension_dir = "C:\software\php\dist\ext"

This made it work for me.

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
catawampus
  • 179
  • 1
  • 3
  • 4
    It worked for me but with `./ext` which is the relative path. And I had to rename `php.ini-development` to `php.ini` and uncomment in that file `extension=php_mysqli.dll`. Oh boy, I did not miss you PHP. – Overdrivr Mar 18 '17 at 12:48
  • The trick here for me (on Windows) was to type in absolute path to extensions dir in "extension_dir". – Niki Romagnoli Oct 15 '19 at 18:29
  • The solution here is for Windows, not for other Os. – Timo Mar 15 '22 at 20:43
12

If you are on Docker...

Inside php-container RUN:

#docker-php-ext-install mysqli

#apachectl restart
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
reyqueson
  • 459
  • 1
  • 7
  • 9
5

My OS is Ubuntu. I solved this problem by using:

sudo apt-get install php5-mysql
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3839088
  • 51
  • 1
  • 1
3

How to Enable mysqli in php.ini

  1. Edit/uncomment by removing ';'(colon) the following config in php.ini: 1st (uncomment and add config):
    include_path = "C:\php\includes"
    
    2nd (uncomment):
    extension_dir = "ext"
    
    3rd (uncomment and edit config):
    extension=C:/PHP/ext/php_mysql.dll
    extension=C:/PHP/ext/php_mysqli.dll
    
  2. Restart the IIS server
  3. Make sure that mysql is running on the system.

How to load php.ini file

  1. Rename any one of the file php.ini-production/php.ini-development to php.ini from C:\PHP(note now the extention will be ini i.e "php.ini").
  2. After renaming to php.ini file restart server
  3. See the changes in http://localhost/phpinfo.php
Dharman
  • 30,962
  • 25
  • 85
  • 135
Gani
  • 422
  • 1
  • 8
  • 16
3
  1. Open your PHP folder.
  2. Find php.ini-development and open it.
  3. Find ;extension=mysqli
  4. delete the ; symbol
  5. save file and change the file extension from php.ini-development to php.ini
  6. Restart the server and test the code:
    if (!function_exists('mysqli_init') && !extension_loaded('mysqli')) {  
            echo 'We don\'t have mysqli!!!';  
     } else {  
         echo 'mysqli is installed';
     }
  1. if it not working, change both extension_dir in php.ini from "ext" to "c:\php\ext" and extension=mysqli to extension=php_mysqli.dll then test again Remember to reset the server every time you test
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Anji
  • 517
  • 2
  • 10
2

For anyone using docker, I ran into this issue, and resolved it by using my own Dockerfile instead of the php:fpm image:

FROM php:fpm

RUN docker-php-ext-install mysqli
bozdoz
  • 12,550
  • 7
  • 67
  • 96
2

Seems like problem with your installation.

  • Have you installed MySQLi?
  • Have you activated it in php.ini?
  • Restarted Apache and/or PHP-FPM?

http://www.php.net/manual/en/mysqli.installation.php

mario
  • 144,265
  • 20
  • 237
  • 291
vartec
  • 131,205
  • 36
  • 218
  • 244
1

I thought I might help anybody with the same problem using Namesco servers. I have been trying to fix this problem after moving a database from my local server on home pc to namesco. They would not assist they said it was a coding issue.

  • However, it was simple to fix from their CPANEl LINUX hosting interface.
  • Click on php.
  • then click on php modules and from their list of preinstalled modules just click the box for mysqli.
  • Then click save. (No need to change code if it worked(s) on another server.)

Unfortunately, their support articles were a waste of time. After reading this I went to admin interface with a new determination.

Dharman
  • 30,962
  • 25
  • 85
  • 135
John
  • 9
  • 2
1

The PHP zip includes most of the commonly used extensions (*.dll on windows such as php_mysqli.dll) under the \ext directory, however they are not enabled by default. You might be getting this Fatal Error when trying to use MySQLi:

( ! ) Fatal error: Uncaught Error: Class 'mysqli' not found in C:\myProject\ class.Database.php on line 24

To enable extensions, open php.ini (you might need to first copy php.ini-development as php.ini), and un-comment (or add) these two lines:

extension_dir = "ext"

And any particular extensions you are getting Fatal Errors for, i.e. for mysqli:

extension=mysqli
Griknok
  • 384
  • 2
  • 13
1

On a fresh install of PHP, remove ; before extension_dir in php.ini.

Andrei
  • 160
  • 2
  • 14
1

All you have to do is go to the php.ini file and on line 926 (depending on version) delete #. It should look like this:

;extension=ldap
;extension=mbstring
;extension=exif
extension=mysqli
;extension=oci8_12c
;extension=odbc

Save the file and open console (cmd) as administrator. Go to the path Apache24 / bin and enter httpd -k restart. This command will restart your server. After that, refresh the page and everything should work.

olios
  • 69
  • 7
0

Some distributions (such as Gentoo) support multiple installations of PHP, and you have to make sure you're using one with mysqli installed and enabled.

On Gentoo, I had installed a new PHP (with the mysqli USE flag enabled), but I needed to activate the new version of PHP (since the old one must have been missing mysqli):

# eselect php list apache2
  [1]   php5.3 *
  [2]   php5.5
# eselect php set apache2 2
You have to run `/etc/init.d/apache2 restart' for the changes to take effect
# /etc/init.d/apache2 restart
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jared Thirsk
  • 1,237
  • 17
  • 17
0

I checked all above and it didn't work for me,

There are some steps I found.

I used PHP Version 5.5.9-1ubuntu4.17 on Ubuntu 14.04

First check the folder

#ls /etc/php5/mods-available/
json.ini  mcrypt.ini  mysqli.ini  mysql.ini  mysqlnd.ini  opcache.ini  pdo.ini  pdo_mysql.ini  readline.ini  xcache.ini

If it did not contain mysqli.ini, read other answer for installing it,

Open php.ini find extension_dir

In my case , I must set extension_dir = /usr/lib/php5/20121212

And restart apache2 : /ect/init.d/apache2 restart

vanduc1102
  • 5,769
  • 1
  • 46
  • 43
0

on ubuntu:

sudo apt-get install php-mysql
sudo service apache2 restart
vishalknishad
  • 690
  • 7
  • 12
0

I found a solution for this problem after a long analysing procedure. After properly testing my php installation with the command line features I found out that the php is working well and could work with the mysql database. Btw. you can run code-files with php code with the command php -f filename.php
So I realized, it must something be wrong with the Apache.
I made a file with just the phpinfo() function inside.
Here I saw, that in the line
Loaded Configuration File
my config file was not loaded, instead there was mentioned (none).

Finally I found within the Apache configuration the entry

<IfModule php5_module>
    PHPINIDir "C:/xampp/php"
</IfModule>

But I've installed the PHP 7 and so the Apache could not load the php.ini file because there was no entry for that. I added

<IfModule php7_module>
    PHPINIDir "C:/xampp/php"
</IfModule>

and after restart Apache all works well.

These code blocks above I found in my httpd-xampp.conf file. May it is somewhere else at your configuration.
In the same file I had changed before the settings for the php 7 as replacement for the php 5 version.

#
# PHP-Module setup
#
#LoadFile "C:/xampp/php/php5ts.dll"
#LoadModule php5_module "C:/xampp/php/php5apache2_4.dll"
LoadFile "C:/xampp/php/php7ts.dll"
LoadModule php7_module "C:/xampp/php/php7apache2_4.dll"

As you can see I have the xampp package installed but this problem was just on the Apache side.

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

on Debian 10

apt install php-mysql

/etc/init.d/apache2 restart
Salvador Rueda
  • 855
  • 2
  • 7
  • 15
0

install phpXX-extension by PHP. In my FreeBSD's case:

pkg install php74-extensions
Dharman
  • 30,962
  • 25
  • 85
  • 135
Biddut Mitra
  • 165
  • 4
0

I'm using xampp and my problem was fixed once i rename:

extension_dir = "ext"

to

extension_dir = "C:\xampp\php\ext"

PS: Don't forget to restart apache after making this change!

Andrei
  • 160
  • 2
  • 14
0

In php.ini :

extension_dir ="c:/wamp64/bin/php/php7.4.9/ext/"

Make sure the PHP version is correct.

Obsidian
  • 3,719
  • 8
  • 17
  • 30
Papa Mike
  • 1
  • 1
-1

When I tried my PHP web app it was giving me the same error.

By googling I found out the MySQL plugin for the PHP server was missing with my installation. I am using Ubuntu, so when I tried MySQL plugin for PHP I found out 2 are there,

php-mysql and php<version>-mysql

I issued command - php --version in my terminal, I was having php7.4,

again I issued apt install php7.4-mysql

It worked.

Willey Hute
  • 939
  • 13
  • 18
-1

This issue can be also caused by the .htaccess file misconfiguration.

16851556
  • 255
  • 3
  • 11
-1

If you tried all answers above and are still out of luck, then do check if the version numbers of the packages installed on your system really match:

apache2-mod_php7-7.4.33-150200.3.46.2.x86_64
php7-mysql-7.4.33-150200.3.46.2.x86_64

Once I had to do a manual installation from a different repo, which lead to certain libs not getting loaded (though their config was OK and their .ini files were getting parsed).

You can also try using

php -S <server_ip>:8080

to launch a test server and see if your missing module (mysqli) loads there. If it shows up in phpinfo there (and doesn't in your regular configuration), then you at least know where to have a look at...