What are the potential pitfalls of using while loops in PHP to iterate over MySQL query results?

Using while loops in PHP to iterate over MySQL query results can potentially lead to performance issues and memory leaks if not handled properly. It is important to free the result set after iterating over it to release memory and resources. Failure to do so can result in increased memory consumption and slower script execution.

// Connect to MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Execute query
$query = "SELECT * FROM table";
$result = mysqli_query($connection, $query);

// Iterate over results
while ($row = mysqli_fetch_assoc($result)) {
    // Process each row
}

// Free result set
mysqli_free_result($result);

// Close connection
mysqli_close($connection);