What are the potential pitfalls of using foreach loops in PHP for database queries?

Using foreach loops for database queries in PHP can lead to performance issues and inefficiencies, especially when dealing with a large number of records. Each iteration of the loop will result in a separate query being executed, which can cause unnecessary overhead. To mitigate this problem, it is recommended to fetch all the data at once using a single query and then iterate over the results.

// Fetch all data at once using a single query
$query = "SELECT * FROM table";
$result = $pdo->query($query);

// Iterate over the results
foreach ($result as $row) {
    // Process each row
}