ARTICLE - Backend Integration

Making PHP 7 Compatible with mysql_connect at the Code Level

PHP 7 removed the mysql_connect family of functions, so legacy code throws a fatal error when called directly. This article explains how to define compatibility functions at the code level that use mysqli internally, letting old code run without major rewrites.

2023-04-03 cnblogs.com (CQZHUOMI)

Making PHP 7 Compatible with mysql_connect at the Code Level

In PHP 7, the mysql family of functions such as mysql_connect is no longer supported. In fact, PHP deprecated these functions back in 5.5 and removed them entirely in 7.0. If a legacy project calls them directly, you get a fatal error similar to:

Uncaught Error: Call to undefined function mysql_connect()

Why This Error Happens

Many small-to-medium sites built around 2015 are littered with mysql_query() and mysql_fetch_array() calls. These projects often run on shared hosting, where the PHP version is controlled by the host. When the host upgrades PHP from 5.x to 7.x, the whole site goes blank overnight and the error logs fill with "Call to undefined function". Even on self-managed servers, many operators simply drop the legacy extension during an upgrade and only discover the damage when production starts failing with mysql calls everywhere in the call chain. Writing a temporary shim is often much faster to restore service than rewriting line by line.

There are two main ways to make PHP 7 compatible with mysql_connect:

Approach How When to use
Install a mysql extension Restore the old functions at runtime (e.g. a mysqlnd compatibility extension) You can modify server configuration
Define functions at the code level Define same-name functions that call mysqli internally You cannot easily change the server environment

Option two requires no extra extension and works by including a small compatibility snippet in the project bootstrap, which is convenient when you cannot easily modify the server environment. Note that this only defines functions with the same names; the underlying operations are still performed with the mysqli family.

Compatibility Code

Place the following code in a common bootstrap file (such as config.php or common.php):

<?php
$dbhost = DATA_HOST;
$dbport = 3306;
$dbuser = DATA_USERNAME;
$dbpass = DATA_PASSWORD;
$dbname = DATA_NAME;

if (!function_exists('mysql_connect')) {
    function mysql_connect($dbhost, $dbuser, $dbpass) {
        global $dbport;
        global $dbname;
        global $mysqli;
        $mysqli = mysqli_connect("$dbhost:$dbport", $dbuser, $dbpass, $dbname);
        return $mysqli;
    }

    function mysql_select_db($dbname) {
        global $mysqli;
        return mysqli_select_db($mysqli, $dbname);
    }

    function mysql_fetch_array($result) {
        return mysqli_fetch_array($result);
    }

    function mysql_fetch_assoc($result) {
        return mysqli_fetch_assoc($result);
    }

    function mysql_fetch_row($result) {
        return mysqli_fetch_row($result);
    }

    function mysql_query($query) {
        global $mysqli;
        return mysqli_query($mysqli, $query);
    }

    function mysql_escape_string($data) {
        global $mysqli;
        return mysqli_real_escape_string($mysqli, $data);
    }

    function mysql_real_escape_string($data) {
        global $mysqli;
        return mysqli_real_escape_string($mysqli, $data);
    }

    function mysql_close() {
        global $mysqli;
        return mysqli_close($mysqli);
    }
}

Note: The original article's mysql_real_escape_string recursively called itself. It has been fixed here to call mysqli_real_escape_string, otherwise it would cause infinite recursion and crash the script.

Common Function Mappings

Legacy function mysqli replacement
mysql_connect mysqli_connect
mysql_select_db mysqli_select_db
mysql_query mysqli_query
mysql_fetch_array mysqli_fetch_array
mysql_fetch_assoc mysqli_fetch_assoc
mysql_fetch_row mysqli_fetch_row
mysql_num_rows mysqli_num_rows
mysql_fetch_object mysqli_fetch_object
mysql_real_escape_string mysqli_real_escape_string
mysql_close mysqli_close

Any other legacy function you encounter can be added the same way: define a same-name wrapper that forwards to its mysqli counterpart.

Key Differences Between mysql and mysqli

The most fundamental difference is connection handling. The legacy mysql extension reused an existing connection when mysql_connect was called with identical parameters, which could plant landmines in long-lived scripts; mysqli opens a fresh connection on every mysqli_connect call, so behavior is more predictable. Second, mysqli exposes both object-oriented and procedural interfaces, and $mysqli->query() is fully equivalent to mysqli_query($mysqli, ...) — so code written in the familiar procedural style migrates with less mental overhead.

Another difference is charset handling. In the mysql era, many projects switched encodings manually with SET NAMES utf8; if you forget after upgrading, Chinese text turns into mojibake. Explicitly calling mysqli_set_charset in the compatibility code is the safest fallback.

Security Reminder

The compatibility code is a stopgap: it keeps legacy concatenated SQL running, but the injection risk is unchanged. If you must keep the shim short-term, at least run all external input — especially anything from $_GET/$_POST spliced directly into queries — through mysql_real_escape_string, and confirm the database account uses least privilege rather than root. Long term, prepared statements are the real answer: placeholders separate parameters from SQL structure and eliminate injection at the root.

Usage Notes

  • The snippet checks if (!function_exists('mysql_connect')) so the functions are only defined when the old functions do not already exist, avoiding conflicts with installed extensions;
  • The wrapper functions keep a single shared connection handle via global $mysqli, so all calls after mysql_connect share the same connection;
  • If your project needs connection parameters beyond the port and database name, add them to the mysqli_connect call;
  • After connecting, add mysqli_set_charset($mysqli, 'utf8mb4') to avoid garbled text — many "data turned into question marks" issues after the PHP 7 upgrade actually have nothing to do with mysql_connect but with an unset charset.

A Real-World Migration Scenario

A secondhand-trading site on shared hosting was forced to PHP 7.2 by the host, and the whole site returned 500s. The fix took two steps: drop the compatibility snippet at the top of config.php, then audit every mysql function call across the files; the site was back within half an hour. Over the next two weekends, the team rewrote hot-path queries as mysqli prepared statements and finally removed the compatibility code.

Migration Advice

Although the compatibility code lets legacy projects run quickly, it is still worth migrating gradually to mysqli or PDO for long-term maintenance:

  1. First use the compatibility code to get the project running on PHP 7 and solve the immediate problem;
  2. Always use mysqli or PDO prepared statements in new code to avoid SQL injection risks, for example:
$stmt = $mysqli->prepare("SELECT * FROM users WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
  1. Replace old calls in batches during free time, and finally remove the compatibility code.

If your project runs on a PHP stack, you can also check the backend and deployment articles in the backend integration category, such as the LNMP stack setup guide, to understand complete PHP and database environment configuration. For database security hardening, see SQL injection defense best practices.

Original post: https://www.cnblogs.com/cqzhuomi/articles/17284418.html (cnblogs.com CQZHUOMI, repost)

Integration & Code Examples

Example 1: A thin PDO compatibility shim to keep legacy code running

<?php
// compat.php — transitional only: maps legacy mysql_* calls onto PDO, never use in new code
final class LegacyMysql
{
    private static ?PDO $pdo = null;

    public static function connect(string $host, string $user, string $pass, string $db): void
    {
        $dsn = sprintf('mysql:host=%s;dbname=%s;charset=utf8mb4', $host, $db);
        self::$pdo = new PDO($dsn, $user, $pass, [
            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES   => false, // real server-side prepares
        ]);
    }

    public static function query(string $sql): PDOStatement
    {
        if (!self::$pdo) {
            throw new RuntimeException('connection not initialised');
        }
        return self::$pdo->query($sql);
    }
}

// Legacy call sites change one line: mysql_query($sql) -> LegacyMysql::query($sql)

The shim only buys "it runs again"; charset=utf8mb4 and EMULATE_PREPARES=false conveniently fix the historical mojibake and injection issues along the way.

Example 2: Replace concatenated SQL with real prepared statements

<?php
// Anti-pattern, common in legacy code:
// $sql = "SELECT * FROM users WHERE email = '" . $email . "'";

// Correct: placeholders with bound parameters
$stmt = $pdo->prepare('SELECT id, email, created_at FROM users WHERE email = :email LIMIT 1');
$stmt->execute([':email' => $email]);
$user = $stmt->fetch();

if (!$user) {
    // Preserve the old behaviour: register on miss, but never swallow database errors
    $insert = $pdo->prepare('INSERT INTO users (email, created_at) VALUES (?, NOW())');
    try {
        $insert->execute([$email]);
    } catch (PDOException $e) {
        // 1062 = duplicate key, meaning a concurrent request beat us to it
        if ($e->errorInfo[1] !== 1062) {
            throw $e;
        }
    }
}

Binding parameters beats escaping strings because prepared statements fix injection, typing and performance at once; treat duplicate-key errors separately instead of as generic failures.

Batching the Move from Compatibility Layer to Prepared Statements

The compatibility layer only makes old calls run; underneath it is still mysqli. The move can go in four batches, each with a checkable completion signal, fixing the white screen before changing call style.

Batch Scope of change How to verify Done when
Stop the bleeding Same-name functions in the entry file, forwarding to mysqli No more Call to undefined function anywhere The site is back, half an hour in the case study
Character set mysqli_set_charset($mysqli, 'utf8mb4') after connecting Chinese text written and read back without question marks Mojibake is gone
New code prepare + bind_param or PDO placeholders only Placeholder queries pass on the test database No concatenated SQL in new commits
Legacy cleanup Old calls replaced in spare windows grep for mysql_ call sites drops batch by batch The compatibility layer is deleted

The connection handle differs: the old mysql extension reused a connection for identical parameters, while mysqli opens a new one on every mysqli_connect; the shim keeps a single handle through global $mysqli so later calls share it.

In the original sample mysql_real_escape_string called itself recursively; it has to point at mysqli_real_escape_string, otherwise the script dies in an infinite recursion.

Give the database account least privilege instead of root. Concatenated SQL is exactly as injectable as before, so external input still needs escaping, and placeholders remain the endpoint.

Limitations

  • This article reflects public information as of 2023-04-03; terms, prices and quotas follow the provider's current published terms.
  • Third-party capability and limit statements follow their official documentation; no further commitment is implied here.
  • Licensing, filing and data-residency decisions must be verified against the regulations of your operating region.

FAQ

What environment does the shim require?

None beyond mysqli. The shim defines the missing functions and forwards them, guarded by function_exists so it never collides with an installed extension. Confirm mysqli is enabled with 1 command, php -m, then set the charset explicitly to avoid mojibake after the upgrade. Two lines of config decide whether it works.

Why do PHP 7 logs fill with undefined function errors?

Because the mysql extension was removed, not merely deprecated. It was marked deprecated in 5.5 and deleted in 7.0, so every legacy call fails at once. Grep the codebase for the whole function family, patch the shared entry file first, then convert the hottest queries.

How does the shim work alongside prepared statements?

The shim restores old calls; new code should bind parameters. Concatenated SQL keeps every injection risk, so still escape external input and grant the database account minimum privileges. Replace calls module by module in quiet windows, then delete the shim once the last 1 is gone.

Related Providers

  • MySQL: the target server when the shim calls mysqli and mysqlnd and charset behaviour need checking.
  • MariaDB: legacy projects on MariaDB where mysqli behaviour differs from the MySQL build.

Community & Resources

Official docs and communities

Learning resources and tools

Original source: cnblogs.com (CQZHUOMI) ↗