What best practices should be followed when using mysql_error() in PHP for error handling in MySQL queries?

When using mysql_error() for error handling in MySQL queries in PHP, it is important to check if there was actually an error before calling mysql_error(). This is because calling mysql_error() without an actual error can lead to potential security risks, as it may expose sensitive information about the database. To ensure safe error handling, always check if the query was successful before calling mysql_error().

$result = mysqli_query($connection, $query);

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