Ffile2fix
Sign in Get started

How to fix "Call to undefined function mysql_connect()"

Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /home/user/public_html/includes/db.php:5

The mysql_* functions were deprecated in PHP 5.5 and removed entirely in PHP 7.0, so any script using them fails on modern PHP. The fix is to migrate the database code to PDO or mysqli, not to downgrade PHP.

Also appears as: Call to undefined function mysql_query() · Call to undefined function mysql_real_escape_string() · Fatal error: Uncaught Error: Call to undefined function mysqli_connect() (mysqli extension missing)

Common causes

  • Legacy code written for PHP 5 running on PHP 7 or 8
  • The host upgraded PHP and old scripts were not updated
  • An abandoned plugin, theme or script that still uses mysql_*
  • For mysqli_connect(): the mysqli extension is not enabled on this PHP build

How to fix it

  1. Confirm which function is missing. If it is mysql_*, the code needs migrating. If it is mysqli_connect(), only the extension is missing: enable mysqli in cPanel Select PHP Version or install php8.x-mysql on a VPS.
  2. Find every mysql_* call. Run grep -rn 'mysql_' --include='*.php' . to list all calls. Typical ones are mysql_connect, mysql_select_db, mysql_query, mysql_fetch_assoc, mysql_num_rows and mysql_real_escape_string.
  3. Replace the connection with PDO. Create one PDO connection with charset utf8mb4 and ERRMODE_EXCEPTION, then pass it to code that needs the database.
  4. Convert queries to prepared statements. Replace string-built SQL and mysql_real_escape_string() with placeholders and execute([$value]). This fixes SQL injection at the same time.
  5. Replace fetch and count functions. mysql_fetch_assoc() becomes $stmt->fetch(PDO::FETCH_ASSOC); mysql_num_rows() becomes rowCount() or a COUNT(*) query.
  6. Do not rely on old PHP or shims. Running PHP 5.6 is unsupported and insecure. Compatibility shims that recreate mysql_* hide the problem and keep injection-prone code alive; use them only as a short-term bridge.

PDO replacement

$pdo = new PDO(
    'mysql:host=localhost;dbname=DB_NAME;charset=utf8mb4',
    'DB_USER', 'DB_PASSWORD',
    [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = ?');
$stmt->execute([$email]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);

How to stop it happening again

  • Scan legacy projects for removed functions before a PHP upgrade
  • Use prepared statements for every query
  • Replace abandoned scripts and plugins with maintained ones

Frequently asked questions

Can I enable the mysql extension on PHP 8?

No. It was removed from PHP itself in 7.0 and there is no official build for PHP 7 or 8. Migrate to PDO or mysqli.

Should I use PDO or mysqli?

Both are fine. PDO has a cleaner prepared-statement API and supports other databases; mysqli is closest to the old functions if you want a quicker line-by-line port.

Is replacing mysql_ with mysqli_ enough?

Not quite: most mysqli procedural functions take the connection as the first argument, and you should switch to prepared statements rather than escaping strings.