How can error reporting be utilized in PHP to debug issues with SQL queries?

When debugging SQL queries in PHP, error reporting can be utilized to identify and resolve issues. By enabling error reporting for SQL queries, any errors that occur during query execution will be displayed, helping to pinpoint the problem. This can include syntax errors, connection issues, or data mismatches.

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

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

// Perform SQL query
$query = "SELECT * FROM table";
$result = $mysqli->query($query);

if (!$result) {
    // Display SQL error if query fails
    echo "Error: " . $mysqli->error;
} else {
    // Process query results
    while ($row = $result->fetch_assoc()) {
        // Do something with the data
    }
}

// Close the database connection
$mysqli->close();