23

I have built PHP 7 with a configuration that worked for a previous version of PHP. Now my WordPress websites get the message:

Your PHP installation appears to be missing the MySQL extension which is required by WordPress.

Other websites using mysqli do work. What am I missing?

I've also included all .so files with mysql in the name:

extension=dba.so
extension=mysql.so
extension=mysqli.so
extension=mysqlnd_mysql.so
extension=mysqlnd_mysqli.so
extension=mysqlnd.so
extension=pdo.so
extension=pdo_mysql.so
extension=pdo_odbc.so
extension=odbc.so
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • There could well be a deluge of Wordpress users facing similar issues. Could this question and answer be a generic catch all? – Progrock Dec 15 '15 at 13:24
  • 1
    Great question btw this can help future people who don't realize the extension was removed in PHP 7.0 – BRoebie Dec 15 '15 at 13:28

6 Answers6

17

PHP 7 has removed mysql_* completely.

You need to use PDO or mysqli. Wordpress seems not to support this.

KiwiJuicer
  • 1,952
  • 14
  • 28
  • the `mysql_*` were removed completely in PHP 7 not only the support of them, correct me if I am wrong – BRoebie Dec 15 '15 at 13:26
  • 5
    I didn't find this answer very helpful. Is PDO or mysqli better for a Wordpress site? What needs to be done to upgrade? – ki9 Oct 13 '16 at 06:36
  • 1
    that's crazy that they don't (still don't in '18) - this should really be sorted by them – treyBake Feb 15 '18 at 08:58
8

mysql_* functions got deleted in PHP 7.0 update your code to mysqli or PDO

Also take a look at prepared statements if you are handling user input. To reduce the chance of SQL injections

An example of mysqli connection string:

<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
?>

An example of pdo connection string:

<?php
    $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
?> 

Note:

That mysqli example handles a connection error

Community
  • 1
  • 1
BRoebie
  • 374
  • 3
  • 13
  • 1
    How can I do that? A quick google doesn't reveal some sort of setting to change it. –  Dec 15 '15 at 13:07
  • no it mysqli is just like `mysql_*` functions just code I'll add an example – BRoebie Dec 15 '15 at 13:11
  • also read the manuals it explains everything you need to know. – BRoebie Dec 15 '15 at 13:15
  • wp-db.php has some interesting variables related to mysqli. –  Dec 15 '15 at 13:33
  • 1
    why the downvote really like to know so I can improve the answers I give in the future :) – BRoebie Dec 15 '15 at 14:21
  • Is PDO or mysqli better for a Wordpress site? What actually needs to be done to upgrade? – ki9 Oct 13 '16 at 06:37
  • @Keith In general PDO is more recommended as it has a better Integration of Prepared Statements and is database engine neutral. What I understand the only thing needed to use one or the other is the upgrade to PHP version 7+ . And of course update the code where required. Now I don't have much experience with WordPress but from what I know WordPress should keep itself up-to-date. If you are not sure if it is check it google for PHP version check WordPress, or something similar. – BRoebie Jul 17 '17 at 11:42
7

As has been mentioned elsewhere, the ext/mysql functions have been removed. We've been talking about this for some time.

ext/mysql was built for MySQL 3.23 and only got very few additions since then while mostly keeping compatibility with this old version which makes the code a bit harder to maintain.

If you're hell-bent on putting them back in, you can add them back to PHP 7 by using the ext/mysql PECL Library

It's important to note that Wordpress 3.9 or later supports mysqli

In WordPress 3.9, we added an extra layer to WPDB, causing it to switch to using the mysqli PHP library, when using PHP 5.5 or higher.

Community
  • 1
  • 1
Machavity
  • 30,841
  • 27
  • 92
  • 100
  • I found this post helpful for adding the mysql extension back to PHP 7: https://ckon.wordpress.com/2015/08/06/put-mysql-functions-back-into-php-7/ – phansen May 02 '16 at 20:23
  • 1
    @phansen All I can say is that's an incredibly bad idea. Migrate to PDO or mysqli. Jumping through hoops to put back deprecated functionality is rarely a good idea. – Machavity May 02 '16 at 20:46
3

Check if the Wordpress still using the Mysql extension that was removed in PHP7.

http://php.net/manual/en/migration70.removed-exts-sapis.php

The Mysqli and PDO extensions were kept. This is the reason your other websites are working.

3

This issue is caused by php 7.1.0-dev.

I built another one with the same configuration version 7.0.0 and the issue was resolved.

This has nothing to do with WordPress since it will automatically try to use MySQLi when MySQL is not found. At least in WP 4.4.

  • ... and if you're not using the latest version of WordPress, you should — because of security issues on previous versions. There _is_ a reason why WordPress is constantly being upgraded :-) – Gwyneth Llewelyn Jun 12 '16 at 21:54
-2

On Ubuntu, I fixed this error by running

sudo apt-get install php-mysql

And then restarting my server (caddy, but you might be using apache or nginx).

source

ki9
  • 5,183
  • 5
  • 37
  • 48