The mysql Extension Is Deprecated and Will Be Removed: Switch to mysqli or PDO
You are encountering a deprecation warning because the mysql_* functions in PHP are officially marked for removal. This extension was originally designed before modern database drivers existed, but it lacks support for prepared statements, does not handle connection pooling efficiently, and offers no protection against SQL injection without manual string escaping. The PHP engine is now issuing a warning to signal that this API is obsolete.
The error you are seeing on line 123 of your file indicates that the code explicitly calls mysql_connect(). While the arguments might be correct for a legacy server, the underlying mechanism is no longer secure or compatible with current PHP versions. The function returns a resource handle that becomes invalid as soon as the script ends or if the connection state changes unexpectedly.
Why This Happens
The deprecation stems from a fundamental shift in how PHP interacts with databases. The old mysql extension was written when SQL injection was less of a concern and server-side prepared statements were not standard practice. Modern development requires robust security measures, which this extension cannot provide out of the box.
Furthermore, the extension does not support multiple active connections per process in the way that modern frameworks expect. It also lacks support for new MySQL features introduced in recent years, such as SSL/TLS encryption by default or multi-threaded connections. The community and the PHP core team have determined that maintaining this legacy code path poses a significant security risk and limits interoperability with other database drivers like PostgreSQL or Oracle.
How to Fix It
You must refactor your code to use either mysqli or PDO. Both provide secure, modern interfaces for database interaction.
Option 1: Using mysqli
The mysqli extension is the direct successor to mysql. You can switch from a procedural style to an object-oriented approach, which is generally more readable and maintainable.
Here is how you would replace the deprecated line:
// Legacy code
// $link = mysql_connect($server, $username, $password);
// Modern mysqli implementation
$host = $server;
$user = $username;
$pass = $password;
$mysqli = new mysqli($host, $user, $pass);
if ($mysqli->connect_error) {
throw new Exception("Connection failed: " . $mysqli->connect_error);
}
With mysqli, you can easily enable prepared statements to prevent SQL injection. This is critical for any application handling user input.
Option 2: Using PDO
PDO (PHP Data Objects) provides a consistent interface that works across multiple database systems, not just MySQL. If your application might need to switch databases in the future, PDO is often the preferred choice.
try {
$pdo = new PDO("mysql:host=$server;dbname=your_database;charset=utf8mb4", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
The Implications of Suppressing Errors
You mentioned the possibility of suppressing deprecation errors by modifying your php.ini file:
error_reporting = E_ALL ^ E_DEPRECATED
While this might remove the warning from your output, it is not a viable long-term solution. Here is what happens if you take this approach:
- Security Blindness: You are ignoring warnings that indicate your code relies on insecure practices. SQL injection vulnerabilities may go undetected because the underlying mechanism (
mysql_*) cannot handle them safely. - Future Breakage: Suppressing errors does not prevent the functions from breaking. In future PHP versions, these functions will simply stop working entirely, causing your application to crash silently or with fatal errors if the warning suppression is removed.
- Maintenance Nightmare: Debugging becomes significantly harder because you lose visibility into why parts of your codebase are failing to meet modern standards.
The only correct course of action is to update your database connection logic to use mysqli or PDO. If you are using a framework like Laravel, this transition is often handled automatically via the database facade, but legacy custom scripts must be updated manually.