Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP 7.1.x - mysqli_connect Isn't Defined (Extension is turned on)

Background

I have WAMP Server (3.0.6) installed on my Windows 10 computer. I am developing a WordPress site using a few custom MySQL tables, so I'm using $wpdb.

Problem

I was running on PHP 7.0.10, and everything was fine. This morning, I installed PHP 7.1.4, and suddenly I got this error:

Fatal error: Uncaught Error: Call to undefined function mysql_connect() in ...\wp-includes\wp-db.php:1573

I dug into it and traced the issue back to the __construct() function, and this if statement:

if ( function_exists( 'mysqli_connect' ) ) {

A var_dump showed me that function_exists( 'mysqli_connect' ) is returning false.

enter image description here

Not the answer

The problem is NOT that I don't have the mysqli extension enabled:

enter image description here

enter image description here

enter image description here

Things That I've Tried

I have switched back to 7.0.x (and the error disappeared), deleted 7.1.4, reinstalled 7.1.4, and switched back to it. It still doesn't work.

I tried 7.1.0, and it doesn't work.

I've tried toggling the extension on and off, and that changes nothing.

I've tried copying the actual php_mysqli.dll file from the working 7.0.10 directory, into the 7.1.4 directory, and that doesn't work.

Edit

Per request, I've added screenshots of what is what is showing from phpinfo(). Under 7.0, I see the info section for mysqli, and under 7.1, the section is missing.

enter image description here enter image description here

like image 614
Andy Mercer Avatar asked May 08 '17 12:05

Andy Mercer


1 Answers

Background

First off, a bit of into about the way that WAMPServer handles php.ini files. If you use phpinfo(), you may notice that the path to the loaded ini file is NOT the ini file in the PHP installation. It instead points to the Apache installation.

enter image description here

But if you look at it, it's a 0KB symlink. What is actually is is a link to an ini file in the PHP installation. But it isn't the php.ini, it instead points to phpForApache.ini. So this:

...\wamp64\bin\apache\apache2.4.23\bin\php.ini

is actually

...\wamp64\bin\php\php[VERSION]\phpForApache.ini

So, you can ignore what is in the Apache folder and focus on your ini files in the PHP folder. However, you can't ignore the php.ini. You need to correct both.

Kudos to Jon Stirling to suggesting checking the loaded configuration file in phpInfo().

What I Did

I installed PHP 7.1 from the PHP website, and forgot to transfer my ini files over from the PHP 7.0 installation. I instead used the default ini files provided by the PHP website.

That didn't work, because there are tweaks in the ini files that are needed to make PHP work with WAMP. So I copied the two ini files over from my 7.0.x folder, and then it started working, mostly, except for the error with mysqli.

Root Cause of my Problem

After scratching at this for an hour, Fred -ii-'s last question finally got me to the answer. I had PHP trying to reference old extension files. Here is why:

The php.ini file and the phpForApache.ini file, provided by WAMP, both hard code some paths. For example, the extensions folder path for PHP 7.0.10 is coded as this:

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

I had copied the ini files over, but they were pointing to the extension folder for 7.0. The dll files for 7.0.x don't work with 7.1.x.

Answer

I went into the two files (php.ini phpForApache.ini) in the PHP 7.1.4 folder, and globally replaced all instances of the text "7.0.10" to "7.1.4". And now it's all working.

like image 111
Andy Mercer Avatar answered Oct 22 '22 04:10

Andy Mercer