How can the PHP function mysql_fetch_assoc() be utilized effectively to handle MySQL query results and prevent errors like "supplied argument is not a valid MySQL result resource"?

When using the `mysql_fetch_assoc()` function in PHP to fetch rows from a MySQL query result, it is important to check if the query execution was successful before attempting to fetch rows. This can be done by verifying that the result resource returned by the `mysql_query()` function is valid. To prevent errors like "supplied argument is not a valid MySQL result resource," you should check if the result resource is valid before passing it to `mysql_fetch_assoc()`.

// Perform the MySQL query
$query = mysql_query("SELECT * FROM table");

// Check if the query was successful
if($query){
    // Fetch rows using mysql_fetch_assoc()
    while($row = mysql_fetch_assoc($query)){
        // Process each row here
    }
} else {
    // Handle query execution error
    echo "Error executing query: " . mysql_error();
}