Deprecated: mysql_connect()

Deprecated: mysql_connect()

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

Stefan Bogdanescu

Founder & Senior Architect

The End of the mysql_* Extension

When you encounter the warning Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future, it is a signal from PHP that your application is running on legacy code that will cease to function eventually. While the script continues to execute, treating this as a mere annoyance is a dangerous strategy. The mysql_* functions are not just deprecated; they are being actively phased out because they lack fundamental security features and do not support modern database protocols effectively.

The warning explicitly points you toward two robust alternatives: mysqli (MySQL Improved) or PDO (PHP Data Objects). Ignoring this message leaves your application vulnerable to SQL injection attacks and ensures it will crash on PHP 8.1+ environments once the deprecation period ends. The mysql_* functions were never designed for prepared statements, making them inherently unsafe for handling user input in queries.

Migrating to mysqli

The most direct replacement involves swapping the procedural mysql_connect calls for their mysqli counterparts. If you are maintaining existing procedural code, the migration path is straightforward but requires updating your connection logic.

Here is how you would refactor your connect.inc.php file to use mysqli:

<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$dbname = 'dbname';

// Create a new mysqli connection
$conn = new mysqli($host, $user, $pass, $dbname);

// Check for connection errors immediately
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Close the connection when done
$conn->close();
?>

This approach offers several advantages over the old mysql_* functions. The mysqli class provides a built-in method to check for errors using $conn->connect_error, which is far more robust than relying on global error reporting flags. Furthermore, mysqli supports prepared statements natively, allowing you to execute queries like $conn->prepare("SELECT * FROM users WHERE id = ?") with bound parameters. This eliminates the risk of SQL injection, a critical security concern that the old extension could not address.

Switching to PDO

For applications that require a more unified interface for accessing different database systems, or if you plan to switch databases in the future, migrating to PDO is often the preferred architectural choice. PDO separates the connection logic from the query execution, providing a consistent API regardless of the underlying database driver.

To refactor your code using PDO, you would initialize a PDO instance and configure it with an array of options for better control over error handling:

<?php
$dsn = 'mysql:host=localhost;dbname=dbname;charset=utf8mb4';
$username = 'root';
$password = '';

try {
    $pdo = new PDO($dsn, $username, $password);
    
    // Enable PDO exceptions for easier debugging
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // Execute a prepared statement safely
    $stmt = $pdo->prepare("SELECT * FROM users WHERE id = :id");
    $stmt->execute([':id' => 1]);
    $user = $stmt->fetch();

} catch (PDOException $e) {
    die("Database error: " . $e->getMessage());
}
?>

Using PDO also simplifies transaction management and prepared statement handling across different drivers. However, it requires a slightly more verbose syntax for simple operations compared to the procedural mysqli style if you are not using object-oriented methods.

Why You Cannot Ignore the Warning

The warning message stating that the extension "will be removed in the future" is accurate. As of PHP 8.1, the legacy mysql_* extension has been completely removed from the core distribution. This means that any code relying on mysql_connect, mysql_query, or similar functions will result in a fatal error, halting execution entirely.

Continuing to run an application with deprecated extensions creates a false sense of security. The code works today only because the PHP interpreter is currently executing it as a legacy fallback. Once you update your PHP version or switch hosting providers that enforce modern standards, your application will break.

If you are building new applications or maintaining existing ones, adopting mysqli or PDO should be part of your immediate technical debt reduction plan. For developers working within the Laravel ecosystem, these migrations align perfectly with how framework database connections are managed out of the box. If you need guidance on integrating these patterns into larger frameworks or optimizing your database connection strategies further, resources like https://laravelcompany.com offer valuable insights into modern PHP development practices.

Our network