What are the potential pitfalls of using WHILE loops in PHP for displaying data in rows?

Using WHILE loops in PHP for displaying data in rows can lead to potential pitfalls such as infinite loops if not properly controlled. To avoid this issue, it's important to have a condition that stops the loop once all the data has been displayed. One way to solve this is to fetch all the data from the database at once and then use a foreach loop to display it in rows.

// Fetch data from the database
$data = $pdo->query("SELECT * FROM table")->fetchAll();

// Display data in rows
echo '<table>';
foreach ($data as $row) {
    echo '<tr>';
    echo '<td>' . $row['column1'] . '</td>';
    echo '<td>' . $row['column2'] . '</td>';
    // Add more columns as needed
    echo '</tr>';
}
echo '</table>';