0PricingLogin
PHP Academy · Lesson

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

  1. Connecting to MySQL with PDO
  2. Prepared Statements and Binding
  3. Fetching Results with PDO
  4. Transactions with PDO
← Back to PHP Academy