What are the advantages and disadvantages of storing database records in an array before outputting them in a while loop in PHP?

Storing database records in an array before outputting them in a while loop can improve performance by reducing the number of database queries. However, this approach may consume more memory, especially if the dataset is large. It can also make the code harder to maintain and update if the array structure needs to be changed.

// Storing database records in an array before outputting them in a while loop
$records = [];
$result = mysqli_query($conn, "SELECT * FROM table");

while ($row = mysqli_fetch_assoc($result)) {
    $records[] = $row;
}

foreach ($records as $record) {
    // Output record data
    echo $record['column_name'] . "<br>";
}