Displaying MySQL Errors for Complex Queries in PHP
When executing complex MySQL queries that rely on user-provided input, it's common to encounter errors that display generic messages such as "Query Failed." To diagnose the root cause of these failures, it's necessary to retrieve the actual error message generated by the database.
In PHP, the easiest way to display the error message is to use the mysqli_error() function. This function returns the last error message associated with the specified MySQL link.
To incorporate this into your code, replace the line:
$r = mysqli_query($this->db_link, $query);
with the following:
$r = mysqli_query($this->db_link, $query) or die(mysqli_error($this->db_link));
This code will display the error message if the query fails.
You can further enhance the error handling by printing the error code using mysqli_errno():
echo mysqli_errno($this->db_link);
The MySQL documentation provides additional details on these functions:
The above is the detailed content of How Can I Display MySQL Error Messages for Complex Queries in PHP?. For more information, please follow other related articles on the PHP Chinese website!