92

I have installed PHP 7 and MySQL 5.5.47 on Ubuntu 14.04 (Trusty Tahr).

I have checked installed extension using:

sudo apt-cache search php7-*

It outputs:

php7.0-common - Common files for packages built from the PHP source
libapache2-mod-php7.0 - server-side, HTML-embedded scripting language (Apache 2 module)
php7.0-cgi - server-side, HTML-embedded scripting language (CGI binary)
php7.0-cli - command-line interpreter for the PHP scripting language
php7.0-phpdbg - server-side, HTML-embedded scripting language (PHPDBG binary)
php7.0-fpm - server-side, HTML-embedded scripting language (FPM-CGI binary)
libphp7.0-embed - HTML-embedded scripting language (Embedded SAPI library)
php7.0-dev - Files for PHP7.0 module development
php7.0-dbg - Debug symbols for PHP7.0
php7.0-curl - CURL module for PHP
php7.0-enchant - Enchant module for PHP
php7.0-gd - GD module for PHP
php7.0-gmp - GMP module for PHP
php7.0-imap - IMAP module for PHP
php7.0-interbase - Interbase module for PHP
php7.0-intl - Internationalisation module for PHP
php7.0-ldap - LDAP module for PHP
php7.0-mcrypt - libmcrypt module for PHP
php7.0-readline - readline module for PHP
php7.0-odbc - ODBC module for PHP
php7.0-pgsql - PostgreSQL module for PHP
php7.0-pspell - pspell module for PHP
php7.0-recode - recode module for PHP
php7.0-snmp - SNMP module for PHP
php7.0-tidy - tidy module for PHP
php7.0-xmlrpc - XMLRPC-EPI module for PHP
php7.0-xsl - XSL module for PHP
php7.0 - server-side, HTML-embedded scripting language (metapackage)
php7.0-json - JSON module for PHP
php-all-dev - package depending on all supported PHP development packages
php7.0-sybase - Sybase module for PHP
php7.0-sqlite3 - SQLite3 module for PHP
php7.0-mysql - MySQL module for PHP
php7.0-opcache - Zend OpCache module for PHP
php7.0-bz2 - bzip2 module for PHP

I am not able to see the MySQLi extension using phpinfo() either. How can I enable/install MySQLi extension in PHP 7?

That's why I cannot use phpMyAdmin. It says "The mysqli extension is missing."

Mohammad Sayeed
  • 2,025
  • 1
  • 16
  • 27

8 Answers8

146

The problem is that the package that used to connect PHP to MySQL is deprecated (php5-mysql). If you install the new package,

sudo apt-get install php-mysql

this will automatically update Apache and PHP 7.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Xeno
  • 1,715
  • 3
  • 12
  • 8
  • 17
    This is the right answer for Ubuntu (16.04). Restart apache afterwards: `sudo service apache2 restart` – scoobydoo Jun 03 '16 at 06:14
  • Thanks Xeno. This is valid answer for Ubuntu (16.04), PHP 7.1 @scoobydoo is right. Restart apache after this. – Riz Oct 13 '17 at 11:47
  • 2
    Just as a warning... php-mysql has been deprecated years ago and PHP developers has warned that the extension was going to be removed on PHP 7 for years now. It is recommended to use php-mysqli instead. – raphie May 07 '19 at 07:02
  • 2
    The OP was Ubuntu, not windows. Uncommenting an extension declaration won't do any good if the extension is not there. `apt-get install php7.3-mysqli` will take care of that if it's not. You can find out by creating a web file that just contains `` and search for `mysqli` – Oliver Williams Mar 30 '21 at 13:26
  • @raphie The thing is, this error occurs on Ubuntu 20 after installing php-mysqli (if you list installed packages php-mysqli does not show up); the fix right now is to `apt install php-mysql`. Could be that something is wrong with php-mysqli via the apt package manager: `# sudo apt-get install -y php-mysqli Reading package lists... Done Building dependency tree Reading state information... Done Note, selecting 'php7.4-mysql' instead of 'php-mysqli' php7.4-mysql is already the newest version (7.4.3-4ubuntu2.10). 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.` – Peter Kionga-Kamau May 03 '22 at 18:41
  • Doesn't work anymore `E: Unable to locate package php7.3-mysqli` and `Package php-mysql is not available, but is referred to by another package. This may mean that the package is missing, has been obsoleted, or is only available from another source` – geoidesic Dec 23 '22 at 16:16
42

For all docker users, just run docker-php-ext-install mysqli from inside your php container.

Update: More information on https://hub.docker.com/_/php in the section "How to install more PHP extensions".

Speedy
  • 1,361
  • 1
  • 14
  • 16
  • 2
    How is someone supposed to find that piece of information?! Is there some doc on that or similar? – Uchendu Jul 08 '20 at 08:51
  • 1
    Speedy u my hero! – Boris Ivanov Jul 12 '20 at 20:15
  • 3
    @Uchendu it's actually right in the descirption of PHP container here https://hub.docker.com/_/php - in the section "How to install more PHP extensions". It took me quite some time to find it too. I've updated my answer to include this information, thank you. – Speedy Jul 18 '20 at 05:27
  • Doesn't work. Using docker on Synology DSM. – Erik Thiart Jan 26 '21 at 21:19
  • @ErikThiart are you using the official php container from the link above? Also, what doesn't work? The command doesn't exist or it returns an error or the package is not installed? Please provide more details. – Speedy Mar 12 '21 at 08:10
  • 1
    This sounds like a fix that applies to a specific docker image and not to dockerized installs in general – Peter Kionga-Kamau May 03 '22 at 18:43
41

I got the solution. I am able to enable MySQLi extension in php.ini. I just uncommented this line in php.ini:

extension=php_mysqli.dll

Now MySQLi is working well. Here is the php.ini file path in an Apache 2, PHP 7, and Ubuntu 14.04 environment:

/etc/php/7.0/apache2/php.ini

By default, the MySQLi extension is disabled in PHP 7.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mohammad Sayeed
  • 2,025
  • 1
  • 16
  • 27
40
sudo phpenmod mysqli
sudo service apache2 restart

  • phpenmod moduleName enables a module to PHP 7 (restart Apache after that sudo service apache2 restart)
  • phpdismod moduleName disables a module to PHP 7 (restart Apache after that sudo service apache2 restart)
  • php -m lists the loaded modules
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pipo
  • 4,653
  • 38
  • 47
31

On Ubuntu, when mysqli is missing, execute the following,

sudo apt-get install php7.x-mysqli

sudo service apache2 restart

Replace 7.x with your PHP version.

Note: This could be 7.0 and up, but for example Drupal recommends PHP 7.2 on grounds of security among others.

To check your PHP version, on the command-line type:

php -v

You do exactly the same if you are missing mbstring:

apt-get install php7.x-mbstring

service apache2 restart

I recently had to do this for phpMyAdmin when upgrading PHP from 7.0 to 7.2 on Ubuntu 16.04 (Xenial Xerus).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
14

In Ubuntu, you need to uncomment this line in file php.ini which is located at /etc/php/7.0/apache2/php.ini:

extension=php_mysqli.so
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Atul Pandya
  • 333
  • 1
  • 7
-2

In case of docker after installing the mysqli as per Speedy's answer above docker-php-ext-install mysqli you should restart the the fpm as per this answer https://stackoverflow.com/a/43076457/932473

From inside container

kill -USR2 1
dav
  • 8,931
  • 15
  • 76
  • 140
-6

Let's use

mysqli_connect

instead of

mysql_connect

because mysql_connect isn't supported in PHP 7.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kratos.vn
  • 25
  • 2