What considerations should be made when handling MySQL result resources in PHP to avoid warnings and errors during data retrieval and processing?

When handling MySQL result resources in PHP, it is important to check if the result is valid before attempting to retrieve data from it. This can help avoid warnings and errors that may occur if the result resource is not valid or if there are no rows returned. Additionally, it is good practice to free the result resource after retrieving the data to release memory.

// Check if the result is valid before retrieving data
if ($result && mysqli_num_rows($result) > 0) {
    // Fetch data from the result
    while ($row = mysqli_fetch_assoc($result)) {
        // Process the data
    }
} else {
    // Handle case where no rows are returned
}

// Free the result resource
mysqli_free_result($result);