How can errors in SQL queries be effectively debugged in PHP, especially when using the mysql_query function?

When debugging errors in SQL queries in PHP, especially when using the mysql_query function, it is important to carefully examine the query syntax and structure for any mistakes. One common approach is to echo the query before executing it to ensure it is correctly formed. Additionally, utilizing error handling functions like mysql_error can help identify any issues with the query execution.

$query = "SELECT * FROM users WHERE id = 1";
echo $query; // Output the query for debugging purposes

$result = mysql_query($query);
if (!$result) {
    die('Error: ' . mysql_error());
}

// Process the query result