PHP mysqli extension in Ubuntu 16.04 not working after upgrade to version 7.0.6

After update this morning php does not work any more. Error:

AH01071: Got error 'PHP message: PHP Fatal error: Call to undefined function mysqli_connect() in /var/www/html/public_html/.../config.php on line 2\n', referer: http://localhost/public_html/

php -v

PHP 7.0.6-9+donate.sury.org~xenial+2 (cli) ( NTS ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

The libraries were installed before, took me quite some time to adapt after dist-upgrading to 16.04


Without more information I can only describe general causes:

  1. php7.0-mysql package is not installed, to fix run sudo apt-get install php-mysql
  2. mysqli module got somehow disabled, to fix run phpenmod mysqli
  3. You are using different web SAPI PHP version than you have PHP CLI, f.e. you might have installed libapache2-mod-php5.6 with php7.0-cli
  4. Your Apache 2.x configuration contains different PHP FPM version (f.e. php5.6-fpm package) and Apache2 module (f.e. libapache2-mod-php7.0). Disable the FPM by running: a2disconf php5.5-fpm.
  5. The web SAPI, you are using for web has different configuration file and thus mysqli module is not loaded, to fix add extension=mysqli.so to this custom configuration file
  6. something else is broken and you should lookup for log messages indicating what's broken

To get more diagnostics:

  1. run `php -r 'phpinfo();' and check the output for MySQL references:

    $ php -r 'phpinfo();' | grep -i mysqli
    /etc/php/5.6/cli/conf.d/20-mysqli.ini,
    mysqli
    MysqlI Support => enabled
    mysqli.allow_local_infile => On => On
    mysqli.allow_persistent => On => On
    mysqli.default_host => no value => no value
    mysqli.default_port => 3306 => 3306
    mysqli.default_pw => no value => no value
    mysqli.default_socket => no value => no value
    mysqli.default_user => no value => no value
    mysqli.max_links => Unlimited => Unlimited
    mysqli.max_persistent => Unlimited => Unlimited
    mysqli.reconnect => Off => Off
    mysqli.rollback_on_cached_plink => Off => Off
    API Extensions => mysql,mysqli,pdo_mysql
    
  2. Do the same with the web, create a simple page with just `' and check the output for MySQLI module

After further looking into the issue, we found out, that there was a mix of PHP 5.5 and PHP 7.0 in the system that probably confused Apache2. As a general recommendation I would definitely recommend start with one PHP version to debug and purge the other(s).

To completely purge a specific PHP version, you can purge phpX.Y-common package that will pull all the other reverse dependencies:

Example:

apt-get purge php5.5-common # to remove PHP 5.5
apt-get purge php5.6-common # to remove PHP 5.6
apt-get purge php7.0-common # to remove PHP 7.0
# and to remove old php5 packages:
apt-get purge php5-common # to remove old PHP 5.x packages