80

For 2 days now I'm trying to solve this, but unfortunately no result. Let me tell you my story about the problem. I've bulid an application on a site, and the application deals with the reviews. But, I'm trying to put it on another site, and I copyed the php files, the sql file from the old site, and moved them to the new site (they are on different FTP servers). When I'm trying to go to the pages from the application, I receive this FATAL ERROR:

Fatal error: Call to undefined function mysqli_connect()

The code that I wrote to connect to the database is this (with hidden credentials):

$con = mysqli_connect("","*the_name*","*the_pass*","*the_database*");
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

Why do I get the error? It works on the old server, and the code I think it's not the problem, because it works on localhost, and on the new server it doesn't. Can anyone help me?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Emi
  • 1,165
  • 4
  • 15
  • 26
  • 7
    looks line in you server mysqli extension is not enabled. check if mysqli is enabled `phpinfo()` – bansi Aug 13 '14 at 08:30
  • 1
    please enable mysqli extension – Agha Umair Ahmed Aug 13 '14 at 08:55
  • I followed below link for my problem : https://askubuntu.com/questions/773601/php-mysqli-extension-in-ubuntu-16-04-not-working-after-upgrade-to-version-7-0-6 – Kodali444 Feb 12 '20 at 16:40
  • 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:40

16 Answers16

69

Simply do it

sudo apt install php-mysqli

It works perfectly and it is version independent

João Pimentel Ferreira
  • 14,289
  • 10
  • 80
  • 109
51

On Windows, the extension extension=php_mysqli.dll can be enabled by removing the semicolon ';' at the beginning of the extension name from the php.ini file.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Dave O'Dwyer
  • 1,172
  • 9
  • 16
25

For CentOS, do:

sudo yum install php-mysqli
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zoran
  • 2,653
  • 2
  • 13
  • 9
14

On Ubuntu I had to install php5 mysql extension:

apt-get install php5-mysql
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Rehmat
  • 2,121
  • 2
  • 24
  • 28
  • 2
    In addition, check that extension "extension=mysqli.so" is enabled (uncommented) in the "/etc/php5/apache2/php.ini" file if you're on Ubuntu/Debian. Then restart the server "service apache2 restart". – Rasta 29 Sep 11 '17 at 19:35
  • This do not work on Ubuntue `E: Package 'php5-mysql' has no installation candidate` – Mahdy H. Dec 19 '21 at 06:28
  • You have to install phpX-mysql where X depends. In my case I ran "php -v" and saw my PHP version was 7.4 and did "sudo apt install php7.4-mysql". You can also do "sudo apt-cache search php | grep mysql" to see which versions are available. – amihart Sep 14 '22 at 02:42
9

Happens when php extensions are not being used by default. In your php.ini file, change
;extension=php_mysql.dll
to
extension=php_mysql.dll.

**If this error logs, then add path to this dll file, eg
extension=C:\Php\php-???-nts-Win32-VC11-x86\ext\php_mysql.dll

Do same for php_mysqli.dll and php_pdo_mysql.dll. Save and run your code again.

RemyG
  • 187
  • 2
  • 4
7

If you have the module installed and set your PHP.INI file properly, check your apache error log for something like the following:

PHP Warning:  PHP Startup: Unable to load dynamic library 'C:\\php\\ext\\php_mysqli.dll' - The specified module could not be found.

In this case, your extension directory is not what you think it is. You may neeed to set it explicitly, like so:

extension_dir="C:\xampp\php\ext"
James John McGuire 'Jahmic'
  • 11,728
  • 11
  • 67
  • 78
  • That was my case, thank you. PS: As documented in php.ini, it's sufficient to add `extension_dir = "ext"` on Windows and `extension_dir = "./"` on the other systems. – Marco Sulla Jan 19 '23 at 13:26
5

Mine was a bit different for php7 on centos7.

In /etc/php.ini

; extension=mysqli

to

extension=mysqli

David Menache
  • 194
  • 2
  • 10
3

On Raspberry Pi I had to install php5 mysql extension.

apt-get install php5-mysql

After installing the client, the webserver should be restarted. In case you're using apache, the following should work:

sudo service apache2 restart
Mohamad Osama
  • 858
  • 9
  • 10
2

If you host the server yourself, and that server happens to be Apache, you can also get this error even if you have uncommented extension=php_mysqli.dll in php.ini.

You also need to tell Apache where to find php.ini by using the PHPIniDir directive in Apache's httpd.conf

AddHandler application/x-httpd-php .php
PHPIniDir "<path-to-folder-where-php-ini-lives>"
AaronDanielson
  • 2,230
  • 28
  • 29
2

if you use ubuntu 16.04 (maybe and above),you have this module already but not enabled by default. Just do this:

sudo phpenmod mysqli
Yu Jiaao
  • 4,444
  • 5
  • 44
  • 57
  • Well this got me some new info... but it seems it is not installed by default on WSL Ubuntu 18.... 'WARNING: Module mysqli ini file doesn't exist under /etc/php/7.2/mods-available' – Dan Ciborowski - MSFT Apr 08 '20 at 21:20
  • 1
    you can install it if missing by `sudo apt-get install php7.2-mysqli` – Yu Jiaao Apr 09 '20 at 12:56
1

There is no error in the, but the mysqli PHP extension is not installed on your machine. Please contact your service provider to fix this issue.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Utkarsh Dixit
  • 4,267
  • 3
  • 15
  • 38
1

Mysqli isn't installed on the new server. Run phpinfo() to confirm.

<?php 

phpinfo();
Goldbug
  • 605
  • 6
  • 8
1

So this may not be the issue for you, but I was struggling with this error. I discovered what was causing my problem, though I can't really explain as to why.

For me, mysqli_connect was working fine where the connection was made on pages in any various sub-directory. For some reason though, the same code referenced on pages in the root directory was returning this error. The strange thing is that it was working fine on my localhost environment in MAMP in the root directory, however on my shared host it was not.

After struggling to figure out what was giving me "Error 500" white screen from this "PHP Fatal Error," I went through the code and stumbled upon this code in the error handling that was suggested by the PHP Manual (https://www.php.net/manual/en/mysqli.error.php).

if (!mysqli_query($link, "SET a=1")) {
    printf("Error message: %s\n", mysqli_error($link));
}

I randomly decided to remove it and, voila, connection to the database working in root directories. Maybe someone smarter than me can explain this, for anyone struggling with a similar issue.

willy
  • 33
  • 10
1

I followed below link to fix this problem. Make sure to enable following Module.
php -r 'phpinfo();' | grep -i mysqli
Link : https://askubuntu.com/questions/773601/php-mysqli-extension-in-ubuntu-16-04-not-working-after-upgrade-to-version-7-0-6

Kodali444
  • 1,283
  • 1
  • 17
  • 21
0

If you get here trying to setup a GitHub Codespaces with Docker and devcontainer.json, update the Dockerfile to the following, with the last 'RUN' line being the key line.

FROM mcr.microsoft.com/vscode/devcontainers/php:0-8.2

# Install system dependencies
RUN apt-get update && apt-get upgrade -y

# Install MySQL extension
RUN docker-php-ext-install mysqli pdo_mysql && docker-php-ext-enable mysqli
jiminy
  • 1,612
  • 3
  • 18
  • 21
-1

In my windows 11 I am using XAMPP 8.2.4

In my case the php.ini is located inside <drive-latter>:\xampp\php\windowsXamppPhp folder

And the extension was looked like this ;extension=mysqli

Just remove the ; and restart the server.

Addition:

When I was using php artisan serve in terminal for Laravel project, it was using <drive-latter>:\xampp\php\windowsXamppPhp\php.ini file.

And when I was using localhost/project_name/public from address bar, it was using <drive-latter>:\xampp\php\php.ini file.

You can also run php --ini in a terminal to see which files are used by PHP in CLI mode.

smartrahat
  • 5,381
  • 6
  • 47
  • 68