What are the best practices for handling MySQL result resources in PHP queries to avoid errors?

When working with MySQL result resources in PHP queries, it is important to properly handle the resources to avoid errors such as memory leaks or resource exhaustion. To avoid these issues, it is recommended to always free the result resource after fetching the data by using the mysqli_free_result() function.

// Perform a MySQL query
$result = mysqli_query($conn, "SELECT * FROM table");

// Fetch the data
while ($row = mysqli_fetch_assoc($result)) {
    // Process the data
}

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