What are some best practices for using fetchAll in PHP database queries?

When using fetchAll in PHP database queries, it is important to handle large result sets efficiently to avoid memory issues. One best practice is to limit the number of rows fetched at a time by using the fetchAll method with a fetch style parameter such as PDO::FETCH_NUM or PDO::FETCH_ASSOC. Another best practice is to iterate over the result set using a foreach loop instead of storing the entire result set in memory.

// Example of using fetchAll with a fetch style parameter and iterating over the result set
$stmt = $pdo->query("SELECT * FROM table");
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($results as $row) {
    // Process each row here
}