Source. This article is based on PHP manual: mysql_connect(). Details were checked against it on November 4, 2026; check the source for later changes.
You move a site from PHP 5.6 to PHP 7 or 8, open it, and get a blank page. The error log says:
PHP Fatal error: Uncaught Error: Call to undefined function mysql_connect()
Nothing is misconfigured. The old mysql_* functions no longer exist. The PHP manual says the extension "was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0", and that MySQLi or PDO_MySQL should be used instead. You cannot switch it back on in PHP 7 or 8, so the code has to change. This guide covers finding every call, choosing a replacement and converting the code safely.
What changed, and when
PHP 5.5 marked the extension deprecated. PHP 7.0 removed it: the manual's list of removed extensions includes mysql alongside ereg. There was never a PHP 6 release, so the migration guide goes straight from 5.6 to 7.0. Whichever version you are leaving, any call to mysql_connect(), mysql_query() or their relatives stops the request on PHP 7 and later.
Step 1: find every call
Run this from the project root. The pattern matches mysql_ followed by letters and an opening bracket, and does not match mysqli_:
grep -rnE "\bmysql_[a-z_]+\(" --include=*.php --include=*.inc --include=*.phtml . | grep -v "/vendor/"
Then search again without excluding vendor, plugins and themes, because old third-party code often carries its own database calls. Also look for function names passed as strings, for example to call_user_func():
grep -rnE "['\"]mysql_[a-z_]+['\"]" --include=*.php .
Check how many files the hits fall in. If one wrapper class or include holds most of them, converting that single place may fix most of the site. If calls are scattered through hundreds of templates, plan a longer job and budget for testing.
Step 2: choose MySQLi or PDO
The manual's guide to choosing a MySQL API lists both as actively developed and recommended for new projects. Two differences matter here. MySQLi has a procedural interface, so the function names look like the old ones, which makes it the quickest mechanical conversion. PDO has only the object interface, but it is a general database layer, so it is the better choice if you might move to a different database later.
To get a legacy site running on PHP 8, MySQLi in procedural style is usually the shortest route. The examples below use it.
Step 3: convert the calls
Most functions map one to one, but the connection comes first in MySQLi. The manual's signature is mysqli_query(mysqli $mysql, string $query), while the old function took the query first and the connection last. Swapping them by accident is the most common mistake in this kind of conversion.
mysql_connect()andmysql_select_db()become one call:mysqli_connect($host, $user, $pass, $db).mysql_query($sql, $link)becomesmysqli_query($link, $sql).mysql_fetch_assoc(),mysql_fetch_array()andmysql_num_rows()keep their shape:mysqli_fetch_assoc($result)and so on.mysql_insert_id($link)becomesmysqli_insert_id($link).mysql_error()becomesmysqli_error($link). A failed connection is reported bymysqli_connect_error().mysql_real_escape_string()should not be ported at all. Use prepared statements.mysql_result()is usually replaced by fetching the row and reading the column you need.
The manual's own warning on mysqli_query() is that if a query contains variable input, parameterized prepared statements should be used instead. This is the right moment to remove the escaping calls. A typical lookup, before and after:
// before: PHP 5.x
$link = mysql_connect($host, $user, $pass);
mysql_select_db($db, $link);
$id = mysql_real_escape_string($_GET['id']);
$res = mysql_query("SELECT title FROM posts WHERE id = '$id'", $link);
while ($row = mysql_fetch_assoc($res)) {
echo $row['title'];
}
// after: MySQLi with a prepared statement
$link = mysqli_connect($host, $user, $pass, $db);
$stmt = mysqli_prepare($link, 'SELECT title FROM posts WHERE id = ?');
mysqli_stmt_bind_param($stmt, 's', $_GET['id']);
mysqli_stmt_execute($stmt);
$res = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_assoc($res)) {
echo $row['title'];
}
mysqli_stmt_get_result() depends on the mysqlnd driver. If your host does not have it, use mysqli_stmt_bind_result() instead.
Step 4: what trips people up after the swap
- The implicit connection is gone. The old functions could omit the connection and use the last one opened. Procedural MySQLi needs it passed every time, so code that relied on a global connection needs the
$linkvariable made available, usually as a parameter. - Character set. Set it explicitly after connecting, for example
mysqli_set_charset($link, 'utf8mb4'), and check it against what your tables use. Garbled accents after an upgrade often come from a mismatch here. - Failures are easy to miss. Check the return value of
mysqli_connect()andmysqli_query(), and logmysqli_error($link)when they fail. A query that returns false and is then passed to another function is the start of the problem described in our guide to the count() TypeError in PHP 8. - Everything that talks to the database. Cron scripts, admin tools, import scripts and old plugins need converting too. A search that covers only the web root will miss them.
Step 5: test before you cut over
Do the conversion on a copy, run it on PHP 8 and walk through every path that touches the database: sign-in, search, any form that saves data, reports and the admin area. Watch the error log as you do. For a large site, our guide to running old and new PHP side by side shows how to move one section at a time, and adding tests to legacy PHP code shows how to build a safety net first. We have not run these snippets against your code, so treat them as a pattern to check on your own staging copy, not a drop-in patch.
When this is a bigger job than a search
If the calls number in the hundreds, sit in code nobody has touched in years, or the site also depends on other removed features, the database layer is usually the first of several upgrades. Our legacy system support work covers this: converting the layer, testing it and moving the platform to a supported PHP version. See also what else breaks between PHP 5.6 and PHP 8 and which PHP versions are still supported.
Other errors from the same upgrade: old-style constructors that never run and PHP 8 undefined variable and array key warnings.
Frequently asked questions
Is there a PHP 6 to PHP 8 upgrade?
There was never a PHP 6 release. If someone says a site runs PHP 6, check with php -v. The manual's migration guide goes directly from 5.6.x to 7.0.x.
Can I re-enable the mysql extension in PHP 7 or 8?
No. The manual says it was removed in PHP 7.0.0 and points to MySQLi or PDO_MySQL instead.
Should I use MySQLi or PDO?
Both are actively developed. MySQLi has a procedural interface and is the quickest conversion from mysql_* code. PDO suits you better if you may change database later.
Stuck with a site full of mysql_ calls?
We convert the database layer, test it on a copy and move your platform to a supported PHP version.
See our Legacy System Support