How can error reporting be implemented in PHP to debug issues with MySQL queries?
When debugging MySQL queries in PHP, it is important to enable error reporting to catch any issues that may arise during query execution. This can be done by setting the error reporting level to include warnings and errors related to MySQL queries. By doing so, any errors encountered during query execution will be displayed, making it easier to identify and fix issues.
// Enable error reporting for MySQL queries
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");
// Execute MySQL query
$query = "SELECT * FROM table";
$result = $mysqli->query($query);
// Check for errors
if (!$result) {
echo "Error: " . $mysqli->error;
} else {
// Process query results
}