We are aware of a potentially service impacting issue. Learn more

How do I convert my old website code that has lots of MySQL errors? Print

  • wordpress, wordpress toolkit, mysql, mysqli, code, website, web server
  • 0

How do I convert my old website code that has lots of MySQL errors?

Short answer: there is no reliable, automated, one‑click converter that will take old mysql_* code and transform it into modern, PHP‑8‑compatible MariaDB‑safe code. But there are tools that can help with parts of the migration; here is what actually works in practice.


 Why no full automatic converter exists

The old mysql_* API wasn’t just deprecated — it was removed in PHP 7.0. The API was:

  • non‑object‑oriented

  • inconsistent

  • missing prepared statements

  • often embedded SQL directly in function calls

Modern PHP (7–8.4) requires either:

  • mysqli (procedural or OO), or

  • PDO (recommended)

A converter would need to:

  • rewrite function names

  • restructure code flow

  • extract SQL strings

  • rebuild prepared statements

  • handle escaping logic

  • detect error‑handling patterns

  • detect resource vs. object usage

That’s not something a regex‑based tool can do safely.


 Tools that do exist (and what they can and cannot do)

✔️ 1. php7cc / php7mar / rector

These tools detect deprecated features but do not rewrite mysql_ calls*.

  • php7cc: flags incompatible code

  • php7mar: similar, but more detailed

  • Rector: can rewrite some legacy patterns, but no official mysql_ → PDO/mysqli rule*

Rector can be extended with custom rules, but that becomes a coding project.


✔️ 2. mysql-to-mysqli converter scripts (partial)

There are a few old GitHub projects like:

  • mysql-to-mysqli

  • php-mysql-mysqli-converter

These attempt simple string replacements such as:

mysql_query($sql) → mysqli_query($conn, $sql)

But they fail on:

  • missing connection variables

  • nested queries

  • error handling

  • mysql_real_escape_string()

  • mysql_fetch_*() differences

  • prepared statements (which mysqli supports but mysql_* never did)

These tools are useful only as linting helpers, not production‑safe converters.


✔️ 3. IDE-assisted refactoring

PhpStorm and VSCode extensions can:

  • highlight deprecated calls

  • help you rewrite them manually

  • detect unsafe SQL patterns

This is the most reliable semi‑automated path.


 What actually works in real migrations

The approach that consistently works is:

1. Decide on mysqli vs PDO

For MariaDB 11.4 under PHP 8.4:

  • PDO is the cleanest long‑term choice

  • mysqli is fine if you want minimal structural change

2. Build a small translation layer

A drop‑in shim like:

function db_query($sql, $params = []) {
    global $pdo;
    $stmt = $pdo->prepare($sql);
    $stmt->execute($params);
    return $stmt;
}

Then rewrite old calls incrementally:

mysql_query("SELECT * FROM users")

becomes:

db_query("SELECT * FROM users");

This lets you modernize the codebase without rewriting everything at once.

3. Use a script to locate mysql_ calls*

A simple grep or PHP tokenizer script can enumerate all deprecated calls so you can triage them.


 


Was this answer helpful?

« Back