Warning: mysql_connect() is deprecated
PHP Programming Language
Severity: CriticalWhat Does This Error Mean?
This warning means your PHP code is using the old 'mysql_' functions to connect to a database. Those functions were deprecated in PHP 5.5 and completely removed in PHP 7. If you see this warning, your server is still running PHP 5. If you upgraded to PHP 7 or 8, the script fails entirely because mysql_connect() no longer exists.
Affected Models
- PHP 5.3
- PHP 5.4
- PHP 5.5 (deprecated)
- PHP 5.6 (deprecated)
- Removed in PHP 7.0+
Common Causes
- The code was written years ago using the old mysql_* extension which is now obsolete
- A legacy CMS, plugin, or script has not been updated to use the modern MySQLi or PDO extension
- The server was recently upgraded from PHP 5 to PHP 7 or 8, breaking old mysql_* calls
- A developer copied old code from a tutorial or forum post that used the outdated extension
- Third-party code (themes, plugins, scripts) was not checked for PHP 7 compatibility before use
How to Fix It
-
Replace mysql_connect() with mysqli_connect(). The parameters are the same: host, username, password, database.
Example: $conn = mysqli_connect('localhost', 'user', 'password', 'database');
-
Replace all mysql_query($sql) calls with mysqli_query($conn, $sql) — you must pass the connection as the first parameter.
Every mysql_* function has a direct mysqli_* equivalent. The main difference is passing the connection object.
-
Replace mysql_fetch_array() with mysqli_fetch_array(), mysql_num_rows() with mysqli_num_rows(), and so on.
The naming pattern is consistent: just add an 'i' after 'mysql'. Most replacements are one-for-one.
-
Consider switching to PDO (PHP Data Objects) instead of MySQLi. PDO works with multiple database types and supports prepared statements more cleanly.
PDO is the modern standard for database access in PHP. It is slightly more complex to set up but more flexible long-term.
-
Do not upgrade to PHP 7 or 8 until you have replaced all mysql_* calls. The functions were completely removed — your site will break immediately.
Run a search across all your PHP files for 'mysql_' to find every call that needs updating.
When to Call a Professional
Migrating from mysql_* to MySQLi or PDO requires rewriting all database code. For a large legacy application, this is significant work. A PHP developer can do the migration systematically and test each change. Do not delay this — running PHP 5 on a live server is a serious security risk.
Frequently Asked Questions
Why was the mysql_ extension removed?
The old mysql_ extension was ancient, insecure, and did not support modern MySQL features. It had no support for prepared statements, which are essential for preventing SQL injection attacks. PHP developers deprecated it in 2012 and removed it in PHP 7 (released 2015) to push everyone to safer alternatives.
Is MySQLi or PDO better?
Both are secure and modern. MySQLi only works with MySQL databases. PDO works with MySQL, PostgreSQL, SQLite, and more. If you only use MySQL, MySQLi is simpler to migrate to from the old mysql_ extension. PDO is better for long-term flexibility.
Can I keep using PHP 5 to avoid rewriting my code?
No — PHP 5 has been end-of-life since December 2018. It receives no security updates. Running PHP 5 on a live website exposes your server and users to known vulnerabilities. Upgrade your code to work with PHP 7 or 8 as soon as possible.