A Complete Guide to Enabling MySQLi Extension in PHP 7 on Ubuntu

Nov 15, 2025 · Programming · 11 views · 7.8

Keywords: PHP | MySQLi | Extension | Enable | Ubuntu | PHP7

Abstract: This article provides a comprehensive guide on enabling the MySQLi extension in PHP 7 on Ubuntu systems, covering methods such as editing the php.ini file, installing packages, and using command-line tools. It includes step-by-step instructions, code examples, verification steps, and discusses the importance of MySQLi and alternative installation approaches to resolve common issues.

Introduction

PHP 7 introduces significant performance and security enhancements over previous versions, but users often face issues with missing extensions, particularly the MySQLi extension. MySQLi (MySQL Improved) provides an improved interface for interacting with MySQL databases, supporting object-oriented and procedural programming, prepared statements, transactions, and other advanced features. It is essential for applications like phpMyAdmin. On Ubuntu systems such as 14.04, the MySQLi extension may not be enabled by default, leading to application failures. This guide details how to enable the MySQLi extension based on community best practices, ensuring a fully configured PHP environment.

Problem Description

After installing PHP 7 and MySQL 5.5.47 on Ubuntu 14.04, users may find that the MySQLi extension is not loaded, as indicated by the absence of MySQLi in phpinfo() output or error messages such as "The mysqli extension is missing." When searching with package managers, packages like php7.0-mysql might be available, but the MySQLi extension requires additional steps for activation. This issue not only affects database connectivity but can also prevent dependent applications like phpMyAdmin from functioning properly.

Solution Overview

The primary methods to enable the MySQLi extension include modifying the php.ini configuration file, installing relevant packages, or using command-line tools. Editing php.ini is the most direct and commonly used approach, as it allows precise control over extension loading. Alternative methods such as installing the php-mysql package or using the phpenmod command serve as supplements for different scenarios. This guide focuses on the php.ini method and provides brief explanations of other options.

Step-by-Step Guide to Enable MySQLi via php.ini

To enable the MySQLi extension, start by editing the PHP configuration file php.ini. On Ubuntu systems, this file is typically located at /etc/php/7.0/apache2/php.ini for Apache servers. Follow these detailed steps:

  1. Open the php.ini file with a text editor. For example, run sudo nano /etc/php/7.0/apache2/php.ini in the terminal. Use administrative privileges to avoid permission issues.
  2. Search for the line related to the MySQLi extension. This line usually starts with extension= followed by the extension filename. On Linux systems, extensions use the .so suffix, so the correct line might be extension=mysqli.so. If the line is commented out (preceded by a semicolon ;), remove the semicolon to uncomment it. For instance, change ;extension=mysqli.so to extension=mysqli.so.
  3. Save the file and exit the editor. In nano, press Ctrl+X, then enter Y to confirm saving.
  4. Restart the Apache server to apply the changes: sudo service apache2 restart. This reloads the PHP configuration and ensures the extension is activated.
  5. Verify that the extension is enabled. You can run php -m | grep mysqli to check the module list, or create a PHP file that calls phpinfo() and view the output in a browser. If the MySQLi extension is loaded, relevant sections will appear in the phpinfo() page.

This method leverages PHP's configuration mechanism, allowing direct control over extension loading and is suitable for most standard installation environments. If the extension file is missing, it may be necessary to install the relevant package first.

Alternative Methods

If editing the php.ini file does not work, consider the following alternatives:

These methods offer flexibility, allowing users to choose the most appropriate approach based on their environment. For example, command-line tools may be more convenient in shared hosting or Docker setups.

Code Examples and Verification

To test if the MySQLi extension is functioning correctly, write a simple PHP script. The following example demonstrates how to establish a database connection and check for errors:

<?php
$mysqli = new mysqli("localhost", "username", "password", "database");
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}
echo "Connected successfully";
?>

Replace username, password, and database with actual database credentials. If the connection is successful, the output "Connected successfully" indicates that the MySQLi extension is enabled and working. Additionally, use the command php -r "if (extension_loaded('mysqli')) echo 'MySQLi loaded';" for a quick verification of the extension status.

Importance and Features of the MySQLi Extension

The MySQLi extension, as an improvement over the older MySQL extension, offers several advantages: support for prepared statements (which help prevent SQL injection), transaction handling, multiple query execution, and an object-oriented interface. From the reference article, MySQLi also includes features for metadata retrieval, error handling, and character set configuration, enhancing the security and efficiency of database operations. In PHP 7, enabling the MySQLi extension is crucial for modern web applications, as it ensures compatibility with the latest MySQL features and provides better performance.

Conclusion

Enabling the MySQLi extension in a PHP 7 environment is a straightforward yet essential task. By editing the php.ini file, installing packages, or using command-line tools, users can quickly resolve extension missing issues. This guide provides detailed steps and code examples to assist with configuration in various scenarios. Once enabled, applications can leverage the advanced features of MySQLi, improving the reliability and security of database interactions. It is recommended to always verify the configuration after changes to avoid potential problems.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.