What are best practices for handling MySQL result resources in PHP to avoid errors like "supplied argument is not a valid MySQL result resource"?

When working with MySQL result resources in PHP, it's important to properly check if the query was successful before trying to use the result resource. This error typically occurs when the query fails and the result resource is not valid. To avoid this error, always check if the query was successful before using the result resource.

// Example of handling MySQL result resources in PHP to avoid errors
$query = "SELECT * FROM users";
$result = mysqli_query($connection, $query);

if ($result) {
    // Process the result resource here
    while ($row = mysqli_fetch_assoc($result)) {
        // Do something with the data
    }
} else {
    // Handle the case where the query failed
    echo "Error: " . mysqli_error($connection);
}