Article

Replacing mysql_connect

Remove mysql_connect() safely: lifecycle, modern replacements, and migration examples for legacy PHP apps.

mysql_connect() opened a link to a MySQL server via ext/mysql. Persistent variants and silent connection reuse patterns were common in PHP 5 apps and are incompatible with modern pooling expectations.

Lifecycle (php.net)

  • Deprecated: ext/mysql deprecated as of PHP 5.5.0
  • Removed: PHP 7.0.0

Modern replacement

PDO::__construct() with DSN charset=utf8mb4, or mysqli_connect / new mysqli(). Enable exceptions.

Migration example

# LEGACY — mysql_connect()
$link = mysql_connect('localhost', $user, $pass);
mysql_select_db('app', $link);

# MODERN
$pdo = new PDO(
    'mysql:host=localhost;dbname=app;charset=utf8mb4',
    $user,
    $pass,
    [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);

Scan before cutover

Search the tree for mysql_connect() and paste samples into the PHP Version Compatibility Checker and PHP Deprecated Checker.

Related tools

Related reading