What are the potential pitfalls of using mysql_fetch_array within a foreach loop in PHP?

Using mysql_fetch_array within a foreach loop in PHP can lead to unexpected behavior because the function fetches a row from a result set and moves the internal data pointer forward. This means that the foreach loop may not iterate over all the rows in the result set as expected. To solve this issue, it is recommended to fetch all rows into an array first and then iterate over that array using a foreach loop.

// Fetch all rows into an array
$result = mysql_query("SELECT * FROM table");
$rows = array();
while ($row = mysql_fetch_array($result)) {
    $rows[] = $row;
}

// Iterate over the array using foreach loop
foreach ($rows as $row) {
    // Process each row here
}