mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

This error message indicates that your application is relying on the mysql_* functions, a legacy API introduced in PHP 4.23. While this library once served as the standard for database interaction, it has significant security flaws and architectural limitations that make it unsuitable for modern web development. The core issue is not necessarily your PHP version, but rather the fact that the extension itself is deprecated. It lacks support for prepared statements, meaning it is inherently vulnerable to SQL injection attacks. Furthermore, it does not support multi-statement queries, which hampers performance and code readability in complex applications.

The error occurs because the mysql_* extension was removed in PHP 7.0. If you are running a version of PHP 5.x, you will see this deprecation warning rather than a fatal error, but relying on it is still a bad practice. The recommended path forward is to migrate to either mysqli (MySQL Improved) or PDO (PHP Data Objects). Both provide robust support for prepared statements and are actively maintained.

Migrating from mysql_* to mysqli

The most direct upgrade involves replacing the procedural mysql_* functions with their mysqli_* counterparts. The syntax changes slightly, but the logic remains similar. You must ensure you are using object-oriented or procedural styles consistently; mixing them is not supported and leads to confusion.

Here is how you can refactor your connection script:

<?php

/* Database config */
$db_host        = 'localhost';
$db_user        = '~';
$db_pass        = '~';
$db_database    = 'banners'; 

/* End config */

// Establish connection
$link = mysqli_connect($db_host, $db_user, $db_pass);

if (!$link) {
    die('Connect Error (' . mysqli_connect_error() . ')');
}

// Set character set and collation
mysqli_set_charset($link, 'utf8mb4');

// Select database
mysqli_select_db($link, $db_database) or trigger_error('Database selection failed', E_USER_ERROR);

// Close connection (optional in OOP style, but good practice here)
// mysqli_close($link); 

?>

Key differences to note: mysql_connect() returns a boolean resource, whereas mysqli_connect() returns an object if the connection is successful and false otherwise. You must handle the error state explicitly using if (!$link) blocks. Additionally, mysql_set_charset() has been replaced by mysqli_set_charset(). It is highly recommended to use utf8mb4 instead of utf8 to ensure full Unicode support for emojis and special characters.

Choosing between mysqli and PDO

While mysqli offers a drop-in replacement with better features, PDO provides a more abstracted interface that works across multiple database drivers (MySQL, PostgreSQL, SQLite, etc.). If your project is strictly MySQL-focused, mysqli is often preferred due to its lower overhead and familiarity. However, if you anticipate switching databases in the future or prefer a unified API for database operations, PDO is the superior choice.

When using PDO, the connection setup looks like this:

<?php
$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION, // Turn on errors
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,       // Set default fetch mode
    PDO::ATTR_EMULATE_PREPARES   => false,                  // Disable emulated prepared statements
];

try {
    $dsn = 'mysql:host=' . $db_host . ';dbname=' . $db_database;
    
    $pdo = new PDO($dsn, $db_user, $db_pass, $options);
    $pdo->exec("SET NAMES 'utf8mb4'"); // Set charset via SQL command
    
} catch (PDOException $e) {
    // Handle exceptions appropriately
    error_log($e->getMessage());
}
?>

Why the Migration Matters

Moving away from mysql_* is not just about avoiding deprecation warnings; it is about securing your application. The legacy API executes queries by string concatenation, which leaves you entirely open to SQL injection if user input is not perfectly sanitized. With mysqli and PDO, prepared statements are the standard mechanism for interacting with data. They separate the SQL logic from the user data at a structural level, rendering injection attacks impossible regardless of input content.

Additionally, the removal of the mysql_* extension in PHP 7.0 signals that the community has moved on from maintaining this legacy code. Continuing to use it means you are running unpatched security vulnerabilities and missing out on performance optimizations available in modern drivers. If you are building a new framework or updating an existing one, adopting mysqli or PDO is the only sustainable path forward. For those looking for managed hosting solutions that handle these modern configurations automatically, platforms like https://laravelcompany.com offer environments pre-configured with up-to-date PHP versions and security patches to ease this transition.

Our network