Fatal error: Call to undefined function mysqli_connect()

PhpMysqli

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

Simply do it

sudo apt install php-mysqli

It works perfectly and it is version independent

Solution 2 - Php

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.

Solution 3 - Php

For CentOS, do:

sudo yum install php-mysqli

Solution 4 - Php

On Ubuntu I had to install php5 mysql extension:

apt-get install php5-mysql

Solution 5 - Php

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.

Solution 6 - Php

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"

Solution 7 - Php

Mine was a bit different for php7 on centos7.

In /etc/php.ini

; extension=mysqli

to

extension=mysqli

Solution 8 - Php

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>"

Solution 9 - Php

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

Solution 10 - Php

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

Solution 11 - Php

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.

Solution 12 - Php

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

<?php 

phpinfo();

Solution 13 - Php

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.

Solution 14 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEmiView Question on Stackoverflow
Solution 1 - PhpJoão Pimentel FerreiraView Answer on Stackoverflow
Solution 2 - PhpDave O'DwyerView Answer on Stackoverflow
Solution 3 - PhpZoranView Answer on Stackoverflow
Solution 4 - PhpRehmatView Answer on Stackoverflow
Solution 5 - PhpRemyGView Answer on Stackoverflow
Solution 6 - PhpJahmicView Answer on Stackoverflow
Solution 7 - PhpdoapydaveView Answer on Stackoverflow
Solution 8 - PhpAaronDanielsonView Answer on Stackoverflow
Solution 9 - PhpMohamad OsamaView Answer on Stackoverflow
Solution 10 - PhpYu JiaaoView Answer on Stackoverflow
Solution 11 - PhpUtkarsh DixitView Answer on Stackoverflow
Solution 12 - PhpGoldbugView Answer on Stackoverflow
Solution 13 - PhpwillyView Answer on Stackoverflow
Solution 14 - PhpKodali444View Answer on Stackoverflow