Are there any best practices to keep in mind when using fetchAll in PDO for database queries in PHP?

When using fetchAll in PDO for database queries in PHP, it's important to keep in mind that fetchAll retrieves all rows from a result set at once, which can be memory-intensive for large result sets. To avoid memory issues, consider using fetch() in a loop to process one row at a time instead of fetching all rows at once.

// Example of using fetch() in a loop instead of fetchAll

// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Prepare and execute the query
$stmt = $pdo->prepare('SELECT * FROM users');
$stmt->execute();

// Fetch one row at a time in a loop
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    // Process the row data here
    echo $row['username'] . '<br>';
}