How can a PHP developer effectively handle error messages related to dataset changes using mysql_affected_rows?

When handling dataset changes in PHP using mysql_affected_rows, it is important for the developer to check the return value of mysql_affected_rows after executing an INSERT, UPDATE, or DELETE query. This value indicates the number of rows affected by the query, which can be used to determine if the operation was successful or if there was an error.

// Execute an INSERT, UPDATE, or DELETE query
$result = mysql_query($query);

// Check if the query was successful
if(mysql_affected_rows() > 0){
    echo "Query executed successfully. Rows affected: " . mysql_affected_rows();
} else {
    echo "Error executing query: " . mysql_error();
}