What are potential pitfalls when iterating through database rows in PHP?

One potential pitfall when iterating through database rows in PHP is not properly handling errors or exceptions that may occur during the iteration process. To solve this, it is important to implement error handling mechanisms such as try-catch blocks to catch any potential exceptions and handle them appropriately.

try {
    // Database query to fetch rows
    $stmt = $pdo->query("SELECT * FROM table");

    // Iterate through rows
    while ($row = $stmt->fetch()) {
        // Process row data
    }
} catch (PDOException $e) {
    // Handle any database errors
    echo "Error: " . $e->getMessage();
}