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
- Composer.json Validator Validate composer.json structure and common mistakes without running composer install.
- Legacy PHP Risk Checker Paste PHP source for a static scan that classifies removed APIs, deprecated calls, and security-sensitive leg…
- PHP Deprecated Checker Find deprecated functions and patterns in pasted PHP to prioritize modernization work.
- PHP Environment Compare Compare two PHP environment summaries to find directive and extension mismatches.
- PHP Modernization Roadmap Build an ordered migration stage list from your PHP version, framework, Composer, database API, and deploymen…
- PHP Version Compatibility Checker Scan pasted PHP for version-sensitive syntax and APIs to plan upgrades across PHP releases.
Related reading
- Composer Modernization Center Add Composer to legacy PHP projects, migrate includes to autoloading, set platform constraints, and replace a…
- PHP 5 to Modern PHP: Complete Incremental Migration Guide A deep, production-minded path from PHP 5.x codebases to supported PHP 8.x: removed extensions, charset, PDO,…
- PHP Security Modernization for Legacy Applications Upgrade inherited PHP security practices: prepared statements, password hashing, sessions, CSRF, XSS escaping…
- PHP 7 to PHP 8 Migration Guide Deep guide to PHP 8.0 breaking changes that matter for PHP 7 applications, with upgrade tactics through suppo…
- Inheriting a Legacy PHP Application A first-30-days playbook for developers handed an unfamiliar PHP codebase: runtime truth, risk triage, and sa…