Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. PDOStatement::fetch() / fetchAll()

PDOStatement::fetch() / fetchAll() Since: PHP 5(2004)

These methods retrieve data from the result set of a prepared statement. Use fetch() to retrieve one row at a time, or fetchAll() to retrieve all rows at once.

Syntax

// Retrieves one row from the result set.
$stmt->fetch($mode);

// Retrieves all rows from the result set.
$stmt->fetchAll($mode);

// Retrieves the value of a single column.
$stmt->fetchColumn($column_number);

// Returns the number of affected rows.
$stmt->rowCount();

Method List

MethodDescription
fetch($mode)Retrieves the next row from the result set. Returns false when there are no more rows.
fetchAll($mode)Retrieves all rows from the result set as an array. For large datasets, using fetch() in a loop is more memory-efficient.
fetchColumn($column)Returns the value of a specified column from the next row in the result set. Omitting the argument returns the value of the first column.
rowCount()Returns the number of rows affected by the last SQL statement. Use this to verify the results of INSERT, UPDATE, or DELETE operations.

Fetch Mode List

ConstantDescription
PDO::FETCH_ASSOCReturns an associative array keyed by column name. This is the most commonly used mode.
PDO::FETCH_NUMReturns a numerically indexed array keyed by column number.
PDO::FETCH_BOTHReturns both an associative array and a numerically indexed array. This is the default mode.
PDO::FETCH_OBJReturns an anonymous object with column names as properties.
PDO::FETCH_CLASSReturns an instance of the specified class.
PDO::FETCH_COLUMNReturns only the values of a specified column as an array. Used with fetchAll().

Sample Code

<?php
// Use fetch() to retrieve one row at a time.
$stmt = $pdo->prepare("SELECT id, name, email FROM users WHERE active = :active");
$stmt->execute([':active' => 1]);

$user = $stmt->fetch(PDO::FETCH_ASSOC);
echo $user['name']; // Outputs the name from the first row.
echo $user['email']; // Outputs the email from the first row.

// Use fetch() in a loop to process one row at a time.
$stmt->execute([':active' => 1]);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
	echo $row['name'] . ": " . $row['email'] . "\n";
}

// Use fetchAll() to retrieve all rows at once.
$stmt = $pdo->prepare("SELECT * FROM users ORDER BY id");
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo count($users) . " rows found."; // Outputs the total row count.

// Use FETCH_OBJ to retrieve rows as objects.
$stmt->execute();
$user = $stmt->fetch(PDO::FETCH_OBJ);
echo $user->name; // Access columns as object properties.

// Use fetchColumn() to retrieve a single value.
$stmt = $pdo->prepare("SELECT COUNT(*) FROM users WHERE active = :active");
$stmt->execute([':active' => 1]);
$count = $stmt->fetchColumn();
echo "Active users: " . $count; // Outputs something like "Active users: 42".

// Use fetchAll() with FETCH_COLUMN to get a single column as an array.
$stmt = $pdo->prepare("SELECT email FROM users ORDER BY id");
$stmt->execute();
$emails = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
print_r($emails); // Results in a flat array of email addresses.

// Use rowCount() to check how many rows were updated.
$stmt = $pdo->prepare("UPDATE users SET last_login = NOW() WHERE id = :id");
$stmt->execute([':id' => 1]);
if ($stmt->rowCount() > 0) {
	echo "Last login time updated.";
} else {
	echo "No matching user found.";
}

// Use FETCH_CLASS to retrieve rows as class instances.
class User {
	public int $id;
	public string $name;
	public string $email;
}

$stmt = $pdo->prepare("SELECT id, name, email FROM users WHERE id = :id");
$stmt->execute([':id' => 1]);
$stmt->setFetchMode(PDO::FETCH_CLASS, 'User');
$user = $stmt->fetch();
echo $user->name; // Retrieved as an instance of the User class.

Notes

fetch() retrieves one row at a time from the result set, while fetchAll() retrieves all rows at once. When working with large datasets, avoid using fetchAll() as it loads everything into memory. Use fetch() in a loop to process one row at a time instead.

You can set the default fetch mode at connection time using PDO::ATTR_DEFAULT_FETCH_MODE, which saves you from specifying it on every call. In general, PDO::FETCH_ASSOC is the most widely used mode, as it lets you access columns by name intuitively.

To prepare and execute SQL statements, see PDO::prepare() / execute(). To connect to a database, see new PDO(). If you need to run multiple SQL statements together, consider using transactions.

If you find any errors or copyright issues, please .