Skip to content
PHP Ecosystem
Explore

DataReader

API reference › yii › db › DataReader

classyii\db\DataReader
Extendsyii\base\BaseObject
ImplementsIterator, Countable, yii\base\Configurable
Since2.0
Sourceframework/db/DataReader.php

DataReader represents a forward-only stream of rows from a query result set.

To read the current row of data, call read(). The method readAll() returns all the rows in a single array. Rows of data can also be read by iterating through the reader. For example,

$command = $connection->createCommand('SELECT * FROM post');
$reader = $command->query();
while ($row = $reader->read()) {
$rows[] = $row;
}
// equivalent to:
foreach ($reader as $row) {
$rows[] = $row;
}
// equivalent to:
$rows = $reader->readAll();

Note that since DataReader is a forward-only stream, you can only traverse it once. Doing it the second time will throw an exception.

It is possible to use a specific mode of data fetching by setting $fetchMode. See the PHP manual for more details about possible fetch mode.

PropertyTypeDescriptionDefined by
$columnCount read-onlyintThe number of columns in the result set.DataReader
$fetchMode write-onlyintFetch mode.DataReader
$isClosed read-onlyboolWhether the reader is closed or not.DataReader
$rowCount read-onlyintNumber of rows contained in the result.DataReader
MethodDescriptionDefined by
__construct()Constructor.DataReader
__call()Calls the named method which is not a class method.yii\base\BaseObject
__get()Returns the value of an object property.yii\base\BaseObject
__isset()Checks if a property is set, i.e. defined and not null.yii\base\BaseObject
__set()Sets value of an object property.yii\base\BaseObject
__unset()Sets an object property to null.yii\base\BaseObject
bindColumn()Binds a column to a PHP variable.DataReader
canGetProperty()Returns a value indicating whether a property can be read.yii\base\BaseObject
canSetProperty()Returns a value indicating whether a property can be set.yii\base\BaseObject
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
close()Closes the reader.DataReader
count()Returns the number of rows in the result set.DataReader
current()Returns the current row.DataReader
getColumnCount()Returns the number of columns in the result set.DataReader
getIsClosed()Whether the reader is closed or not.DataReader
getRowCount()Returns the number of rows in the result set.DataReader
hasMethod()Returns a value indicating whether a method is defined.yii\base\BaseObject
hasProperty()Returns a value indicating whether a property is defined.yii\base\BaseObject
init()Initializes the object.yii\base\BaseObject
key()Returns the index of the current row.DataReader
next()Moves the internal pointer to the next row.DataReader
nextResult()Advances the reader to the next result when reading the results of a batch of statements.DataReader
read()Advances the reader to the next row in a result set.DataReader
readAll()Reads the whole result set into an array.DataReader
readColumn()Returns a single column from the next row of a result set.DataReader
readObject()Returns an object populated with the next row of data.DataReader
rewind()Resets the iterator to the initial state.DataReader
setFetchMode()Set the default fetch mode for this statement.DataReader
valid()Returns whether there is a row of data at current position.DataReader
public function __construct(yii\db\Command $command, array $config = [])

Constructor.

Parameters

ParameterTypeDescription
$commandCommandThe command generating the query result
$configarrayName-value pairs that will be used to initialize the object properties
public function bindColumn(int|string $column, mixed &$value, int|null $dataType = null): mixed

Binds a column to a PHP variable.

When rows of data are being fetched, the corresponding column value will be set in the variable. Note, the fetch mode must include PDO::FETCH_BOUND.

Parameters

ParameterTypeDescription
$columnint|stringNumber of the column (1-indexed) or name of the column in the result set. If using the column name, be aware that the name should match the case of the column, as returned by the driver.
$valuemixedName of the PHP variable to which the column will be bound.
$dataTypeint|nullData type of the parameter

Returns

mixed

See also

public function close(): mixed

Closes the reader.

This frees up the resources allocated for executing this SQL statement. Read attempts after this method call are unpredictable.

Returns

mixed

public function count(): int

Returns the number of rows in the result set.

This method is required by the Countable interface. Note, most DBMS may not give a meaningful count. In this case, use “SELECT COUNT(*) FROM tableName” to obtain the number of rows.

Returns

int: Number of rows contained in the result.

public function current(): mixed

Returns the current row.

This method is required by the interface Iterator.

Returns

mixed: The current row.

public function getColumnCount(): int

Returns the number of columns in the result set.

Note, even there’s no row in the reader, this still gives correct column number.

Returns

int: The number of columns in the result set.

public function getIsClosed(): bool

Whether the reader is closed or not.

Returns

bool: Whether the reader is closed or not.

public function getRowCount(): int

Returns the number of rows in the result set.

Note, most DBMS may not give a meaningful count. In this case, use “SELECT COUNT(*) FROM tableName” to obtain the number of rows.

Returns

int: Number of rows contained in the result.

public function key(): int

Returns the index of the current row.

This method is required by the interface Iterator.

Returns

int: The index of the current row.

public function next(): mixed

Moves the internal pointer to the next row.

This method is required by the interface Iterator.

Returns

mixed

public function nextResult(): bool

Advances the reader to the next result when reading the results of a batch of statements.

This method is only useful when there are multiple result sets returned by the query. Not all DBMS support this feature.

Returns

bool: Returns true on success or false on failure.

public function read(): array|false

Advances the reader to the next row in a result set.

Returns

array|false: The current row, false if no more row available

public function readAll(): array

Reads the whole result set into an array.

Returns

array: The result set (each array element represents a row of data). An empty array will be returned if the result contains no row.

public function readColumn(int $columnIndex): mixed

Returns a single column from the next row of a result set.

Parameters

ParameterTypeDescription
$columnIndexintZero-based column index

Returns

mixed: The column of the current row, false if no more rows available

public function readObject(string $className, array $fields): mixed

Returns an object populated with the next row of data.

Parameters

ParameterTypeDescription
$classNamestringClass name of the object to be created and populated
$fieldsarrayElements of this array are passed to the constructor

Returns

mixed: The populated object, false if no more row of data available

public function rewind(): mixed

Resets the iterator to the initial state.

This method is required by the interface Iterator.

Returns

mixed

Throws

public function setFetchMode(int $mode): mixed

Set the default fetch mode for this statement.

Parameters

ParameterTypeDescription
$modeintFetch mode

Returns

mixed

See also

public function valid(): bool

Returns whether there is a row of data at current position.

This method is required by the interface Iterator.

Returns

bool: Whether there is a row of data at current position.