How can the mysql_error() function be utilized to troubleshoot SQL syntax errors in PHP scripts?

The mysql_error() function can be utilized to troubleshoot SQL syntax errors in PHP scripts by capturing and displaying any error messages generated by the MySQL database. This can help identify the specific syntax error that is causing the issue in the SQL query and allow for quick debugging and resolution.

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

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