In the context of PHP and MySQL, how can errors in SQL queries be effectively debugged?

When debugging SQL queries in PHP and MySQL, it is helpful to use the `mysqli_error()` function to retrieve detailed error messages. This function can be called after executing a query to check for any syntax errors or other issues that may have occurred. By printing out the error message, it can provide valuable information on what went wrong with the query.

// Connect to MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Check connection
if (!$connection) {
    die("Connection failed: " . mysqli_connect_error());
}

// Execute SQL query
$query = "SELECT * FROM users WHERE id = 1";
$result = mysqli_query($connection, $query);

// Check for errors
if (!$result) {
    echo "Error: " . mysqli_error($connection);
}

// Close connection
mysqli_close($connection);