What are the advantages of using fetch() instead of traditional methods like fetchAll() in PHP for database queries?

When fetching data from a database in PHP, using fetch() instead of fetchAll() can be advantageous when dealing with large result sets. Fetch() retrieves one row at a time, which can be more memory-efficient compared to fetchAll() which retrieves all rows at once. This can be especially useful when working with large datasets that may not fit into memory all at once.

// Using fetch() instead of fetchAll() in PHP for database queries
$stmt = $pdo->query("SELECT * FROM table");

while ($row = $stmt->fetch()) {
    // Process each row individually
    echo $row['column_name'] . "\n";
}