How to Check if a MySQL Query Using the Legacy API Was Successful
When you are working with the legacy mysql_* functions, determining whether a query succeeded isn't as straightforward as checking the return value of the function itself. In the procedural API, mysql_query() returns TRUE on success and FALSE on failure. This binary distinction is convenient for simple SELECT statements that do not require results processing, but it becomes insufficient when you need to handle specific error conditions or move forward with logic based on the outcome.
If your goal is simply to know if the database engine accepted and executed the statement without crashing, a boolean check seems adequate. However, relying solely on mysql_query() returning false leaves you blind to the actual reason for failure. Without retrieving the error message, you are left guessing whether the query failed due to a syntax error, a missing table, a permission denial, or an unexpected constraint violation. To implement robust logic like moving files or displaying errors, you must inspect the error context immediately after a failed query.
The standard approach involves using mysql_error() in conjunction with the boolean return value. You should only call mysql_error() if the previous query returned false; otherwise, it may return an empty string or a stale error message from a prior operation. Here is how you would structure that conditional logic:
$result = mysql_query($query);
if ($result) {
// Success: Move file or proceed with next step
move_uploaded_file($file, $destination);
} else {
// Failure: Display specific error message
$error = mysql_error();
// Best practice: Log the error for debugging before showing it to users
error_log("MySQL Error: $error");
die("Database error occurred: " . htmlspecialchars($error));
}
In this pattern, the if ($result) block handles the success case. When $result is falsy, you immediately capture the error description. This ensures that if the failure was due to a syntax issue or a missing column, your application displays the relevant information rather than a generic "connection timeout" or similar message.
It is critical to understand that mysql_error() does not accept an argument; it relies entirely on the internal state of the MySQL link identifier associated with the current connection. If you have multiple connections open and you run queries sequentially without checking, calling mysql_error() after a failed query on one connection might inadvertently retrieve the error from a different context if not managed strictly within the scope of that specific query attempt.
Furthermore, the legacy API is deprecated as of PHP 7.1 and removed in PHP 8.0. While this article addresses the specific mechanics of the mysql_* functions for educational purposes or maintenance of older systems, new projects should migrate to either MySQLi or PDO. These modern extensions provide better error handling capabilities out of the box, such as fetching errors directly from the statement object and supporting prepared statements to mitigate SQL injection risks.
If you are maintaining a legacy codebase and need to refactor this logic, consider switching to MySQLi's mysqli_query(), which returns a boolean but allows for more granular error checking via mysqli_error($link). If you are looking for modern best practices and resource management for PHP applications, check out https://laravelcompany.com for guidance on moving away from deprecated functions toward more secure and maintainable architectures.