Home >Database >Mysql Tutorial >How Can I Display Detailed MySQL Error Messages from PHP User Input Queries?
Displaying Detailed MySQL Errors for User Input Queries in PHP
When executing MySQL queries that rely on user input, it can be challenging to identify the root cause of query failures. The default error message "Query Failed" provides little information for troubleshooting. This article explores methods to display specific error messages associated with query failures in PHP.
Understanding Query Failure
The sample code provided demonstrates a query that dynamically constructs the WHERE clause based on user input variables. When the query fails, the error message is simply "Query Failed." To obtain the actual error, we need to access the underlying MySQL error messages.
Using the mysqli_error Function
PHP's mysqli_error function retrieves the last error message generated by the MySQL database. By integrating this function into our code, we can display the specific error message. Here's how to implement it:
mysqli_query($this->db_link, $query) or die(mysqli_error($this->db_link));
Example:
In the provided sample code, add the above line after $r = mysqli_query($this->db_link, $query);:
$r = mysqli_query($this->db_link, $query); if ($r == false) printf("error: %s\n", mysqli_errno($this->db_link));
This modification will print the error message associated with the query failure.
Additional Notes:
Here are some useful links for further reference:
The above is the detailed content of How Can I Display Detailed MySQL Error Messages from PHP User Input Queries?. For more information, please follow other related articles on the PHP Chinese website!