You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHP7环境中使用Mysql?兼容旧代码的实现方案咨询

How to Use Legacy mysql_* Functions in PHP7

Hey there, I totally get the frustration—having legacy code that relies on the old mysql_* functions but needing to run it on PHP7 can feel like hitting a wall. Let’s break down practical, low-effort ways to make this work without rewriting half your project:

1. Roll Your Own Compatibility Wrappers (No Extra Extensions Needed)

This is my go-to for small to medium projects. The idea is to create simple wrapper functions that map every mysql_* call your code uses to the equivalent mysqli_* function.

Create a file like mysql_compat.php and drop in these core functions (add more as your project needs them):

<?php
// Mimic mysql_connect
function mysql_connect($host, $user, $pass, $dbname = null) {
    $conn = mysqli_connect($host, $user, $pass);
    if ($dbname && $conn) {
        mysqli_select_db($conn, $dbname);
    }
    return $conn;
}

// Mimic mysql_query
function mysql_query($query, $conn = null) {
    if (!$conn) {
        global $link; // Compatibility with old global connection vars
        $conn = $link;
    }
    return mysqli_query($conn, $query);
}

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

// Add others like mysql_error, mysql_num_rows, mysql_close as needed
?>

Then just require this file at the top of your project’s entry point (like index.php):

require_once 'mysql_compat.php';

This works seamlessly for most legacy code, and you only need to write wrappers for the functions your actual code uses—no wasted effort.

2. Use a Third-Party Compatibility Extension

If your project uses tons of mysql_* functions and writing wrappers feels tedious, try a pre-built extension:

  • mysqlnd_mysql: This is a mysqlnd-based extension that reimplements the full mysql_* API using mysqli under the hood. It’s available via PECL (note: it’s not actively maintained anymore, but it still works reliably on PHP7.x).

Install it on Linux with:

pecl install mysqlnd_mysql

Then add this line to your php.ini and restart your PHP service:

extension=mysqlnd_mysql.so

Once done, all your old mysql_* calls will work exactly as they did in PHP5—no code changes needed.

3. Lightweight Bulk Code Replacement

If extensions and wrappers aren’t your vibe, you can do a global find-and-replace to swap mysql_ with mysqli_, then tweak parameter order where needed. For example:

  • mysql_query($query) becomes mysqli_query($your_connection_var, $query) (you’ll need to make sure your connection is stored in a consistent variable, or use a global $link like old code did)
  • Most fetch functions like mysql_fetch_assoc($result) can be swapped directly to mysqli_fetch_assoc($result) since their parameters match.

This is faster than full rewrites but requires a quick pass to fix connection-related function arguments.


A quick heads-up: The old mysql_* functions are unmaintained and insecure (they’re prone to SQL injection if you’re not careful). While these hacks will get your project running on PHP7, it’s worth prioritizing gradual refactoring to mysqli or PDO with prepared statements for long-term safety and maintainability.

内容的提问来源于stack exchange,提问作者Amey Damle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:16:11