Article

PHP Database Modernization Guide

Move legacy PHP data access from mysql_* to mysqli/PDO with prepared statements, utf8mb4, transactions, and credentials kept outside the webroot.

Database modernization removes the obsolete mysql_* extension, stops string-built queries, and aligns connection charset and SQL modes with what modern MySQL/MariaDB expect. Do this in vertical slices (login, checkout, admin) rather than a single unreviewed rewrite of every query.

LEGACY — mysql_*
MODERN — PDO
 PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    ]
);

$stmt = $pdo->prepare('SELECT id, total FROM orders WHERE user_id = ?');
$stmt->execute([(int) $userId]);

Workstream checklist

  • Prepared statements for every user-influenced query
  • utf8mb4 on connection, tables, and columns
  • Strict SQL modes on staging before production
  • Transactions around multi-step writes
  • Foreign keys and index review where data allows
  • Schema changes via migrations with backups—not ad-hoc production phpMyAdmin clicks
  • Credentials outside the document root / in environment variables

Connection failures belong in logs with DSN host/dbname—not passwords. Related security notes live in PHP security modernization. PDO manual: php.net/pdo.

Related reading