0Pricing
PHP Academy · Lesson

Fetching Results with PDO

Retrieve rows with fetchAll, fetch, and PDO fetch modes.

Fetching Results with PDO is a free PHP Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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";
}

fetchAll()

Returns all rows at once. Convenient for small result sets.

<?php
$stmt = $pdo->query("SELECT * FROM products ORDER BY price");
$products = $stmt->fetchAll();

FETCH_ASSOC

Returns rows as associative arrays keyed by column name — the most common fetch mode.

<?php
$row = $stmt->fetch(PDO::FETCH_ASSOC);
// $row["email"], $row["name"]

FETCH_OBJ

Returns rows as stdClass objects. Access columns as properties.

<?php
$stmt->setFetchMode(PDO::FETCH_OBJ);
$row = $stmt->fetch();
echo $row->email;

FETCH_CLASS

Hydrates rows directly into instances of a given class.

<?php
$stmt->setFetchMode(PDO::FETCH_CLASS, User::class);
$users = $stmt->fetchAll(); // array of User objects

fetchColumn()

Fetches a single column value from the next row. Useful for COUNT queries.

<?php
$count = $pdo->query("SELECT COUNT(*) FROM orders")->fetchColumn();
echo "Total orders: ".$count;

FETCH_KEY_PAIR

Returns an associative array where the first column is the key and the second is the value — perfect for id→name lookups.

<?php
$map = $pdo->query("SELECT id, name FROM categories")
           ->fetchAll(PDO::FETCH_KEY_PAIR);
// $map[1] = "Electronics", $map[2] = "Books"

FETCH_UNIQUE

FETCH_UNIQUE indexes the result by the first column, eliminating duplicates.

FETCH_GROUP

FETCH_GROUP groups rows by the first column value — useful for one-to-many relationships.

closeCursor()

Some drivers require closeCursor() on a statement before running another query on the same connection.

Null Results

fetch() returns false when no more rows exist. Always check: if ($row === false).

Quick Check

Which method returns a single column value from one row?

Frequently asked questions

Is the “Fetching Results with PDO” lesson free?

Yes — the full text of “Fetching Results with PDO” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Fetching Results with PDO”?

Retrieve rows with fetchAll, fetch, and PDO fetch modes. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Fetching Results with PDO” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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