Fetching Results with PDO
Retrieve rows with fetchAll, fetch, and PDO fetch modes.
fetch() vs fetchAll()
fetch() returns one row at a time, moving the cursor forward. fetchAll() returns all rows as an array in one call.
fetch() in a Loop
Ideal for large result sets — processes one row at a time without loading everything into memory.
<?php
$stmt = $pdo->query("SELECT id, name FROM users");
while ($row = $stmt->fetch()) {
echo $row["id"]." - ".$row["name"]."\n";
}All lessons in this course
- Connecting to MySQL with PDO
- Prepared Statements and Binding
- Fetching Results with PDO
- Transactions with PDO