Deprecated: mysql_connect(): The mysql extension is deprecated

PHP Programming Language

Severity: Moderate

What it means

PHP 5.5 and 5.6: Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead.
PHP 7 and later: Fatal error: Uncaught Error: Call to undefined function mysql_connect().
The PHP manual: the extension was deprecated in PHP 5.5.0 and removed in PHP 7.0.0. Use the MySQLi or PDO_MySQL extension instead.

Affected Models

  • PHP 5.5 and later

How to Fix It

  1. Connect with mysqli instead: $db = mysqli_connect('localhost', 'user', 'password', 'database');. The manual names mysqli_connect() and PDO::__construct() as the replacements.

  2. Change the other calls too: mysql_query($sql) becomes mysqli_query($db, $sql), and so on. Most mysqli functions take the connection as their first argument.

  3. Queries built from user input? Move them to prepared statements at the same time, with mysqli_prepare() or PDO::prepare(). They keep the input apart from the SQL.