What are the best practices for iterating through MySQL query results in PHP using while loops?

When iterating through MySQL query results in PHP using while loops, it is important to ensure that you fetch each row of the result set until there are no more rows left to fetch. This is typically done by using the mysqli_fetch_assoc() function within a while loop to fetch each row as an associative array. It is also important to free the result set after iterating through it to release the memory associated with it.

// Assuming $conn is the mysqli connection and $query is the SQL query
$result = mysqli_query($conn, $query);

if ($result) {
    while ($row = mysqli_fetch_assoc($result)) {
        // Process each row here
        echo $row['column_name'] . "<br>";
    }
    
    // Free the result set
    mysqli_free_result($result);
} else {
    echo "Error executing query: " . mysqli_error($conn);
}