PHP PDO how to fetch a single row?

PHP PDO how to fetch a single row?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Fetching a Single Row Efficiently with PDO

When you know your query will return exactly one row, looping over an array of results is unnecessary overhead. The standard pattern for fetching multiple rows uses fetchAll() followed by a foreach loop to iterate through the result set. While this approach scales well for pagination or bulk operations, it allocates memory for every row returned and then iterates sequentially. If your application logic guarantees a single record exists, maintaining that loop introduces latency without adding value.

To optimize this scenario, you should replace fetchAll() with fetch(). The PDOStatement class provides the fetch() method specifically designed to retrieve a single row immediately. This shifts the execution model from building a full array in memory to returning a single object or array as soon as the query completes.

Consider the following implementation using PDO's default fetch style, which returns an associative array:

$STH = $DBH->prepare("SELECT figure FROM table1");
$STH->execute();
$row = $STH->fetch(); // Returns a single row or false if no rows exist

if ($row) {
    echo $row['figure'];
}

This change eliminates the creation of an intermediate array containing all results. Instead, PDO binds the cursor position to the first (and only) row and hands it back directly to your variable. If the database returns zero rows, fetch() returns false. You must handle this case explicitly, as accessing a property on false will trigger a fatal error. The conditional check ensures robustness even when the "single row" assumption is violated by data inconsistencies or missing records.

You can further refine your code by specifying the fetch mode directly in the query execution options or by chaining the method call with style flags. For instance, using PDO::FETCH_OBJ allows you to access properties like $row->figure instead of using array syntax. While this is a stylistic choice, it aligns well when you are processing individual records rather than mapping them to arrays.

However, simply changing the fetch method does not address the root cause if your SQL query lacks constraints. If table1 allows duplicate entries for the figure column, your assumption that only one row exists might be incorrect unless a WHERE clause restricts the result set or a primary key ensures uniqueness. Always verify that your SQL logic enforces the single-row expectation before relying on fetch().

In larger applications, particularly those built with frameworks like Laravel, you often interact with database queries through Eloquent models or query builders. While raw PDO is useful for performance-critical scripts, framework abstraction layers sometimes default to returning collections regardless of row count. When working with raw SQL via PDO as shown above, sticking to fetch() for single-record scenarios keeps memory usage minimal and response times tight.

The trade-off here is negligible in terms of code complexity but significant in terms of resource management. Avoiding the allocation of an empty or single-element array reduces garbage collection pressure and avoids parsing overhead during the iteration phase. For high-traffic endpoints where millisecond latency matters, this micro-optimization accumulates into meaningful performance gains over time.

Our network