How can error reporting in PHP be utilized to troubleshoot issues with database queries?

When troubleshooting database query issues in PHP, error reporting can be utilized to identify the specific errors occurring during query execution. By enabling error reporting and displaying error messages, developers can pinpoint the exact problem within their SQL queries and make necessary adjustments to resolve the issue.

// Enable error reporting for database queries
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// Connect to the database
$connection = new mysqli("localhost", "username", "password", "database");

// Execute the query
$query = "SELECT * FROM table";
$result = $connection->query($query);

// Check for errors
if (!$result) {
    echo "Error: " . $connection->error;
} else {
    // Process the query results
}