How can the mysql_error function be used to troubleshoot SQL syntax errors in PHP?

The `mysql_error` function can be used in PHP to retrieve the error message generated by MySQL when a query encounters a syntax error. By echoing out the error message, developers can quickly identify and troubleshoot SQL syntax errors in their PHP code.

$query = "SELECT * FROM users WHERE username = 'john'";
$result = mysql_query($query);

if (!$result) {
    echo "Error: " . mysql_error();
} else {
    // Process the query result
}