# DataReader

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

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [db](/docs/api/yii/db/index.md) › DataReader

| class | `yii\db\DataReader` |
| --- | --- |
| Extends | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`Iterator`](https://www.php.net/manual/en/class.iterator.php), [`Countable`](https://www.php.net/manual/en/class.countable.php), [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/db/DataReader.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/db/DataReader.php#L54) |

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

To read the current row of data, call [`read()`](/docs/api/yii/db/datareader/index.md#method-read). The method [`readAll()`](/docs/api/yii/db/datareader/index.md#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`](/docs/api/yii/db/datareader/index.md#property-fetchMode). See the [PHP manual](https://www.php.net/manual/en/function.PDOStatement-setFetchMode.php)
for more details about possible fetch mode.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-columnCount"></span>`$columnCount` read-only | `int` | The number of columns in the result set. | `DataReader` |
| <span id="property-fetchMode"></span>`$fetchMode` write-only | `int` | Fetch mode. | `DataReader` |
| <span id="property-isClosed"></span>`$isClosed` read-only | `bool` | Whether the reader is closed or not. | `DataReader` |
| <span id="property-rowCount"></span>`$rowCount` read-only | `int` | Number of rows contained in the result. | `DataReader` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Constructor. | `DataReader` |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`bindColumn()`](#method-bindColumn) | Binds a column to a PHP variable. | `DataReader` |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`close()`](#method-close) | Closes the reader. | `DataReader` |
| [`count()`](#method-count) | Returns the number of rows in the result set. | `DataReader` |
| [`current()`](#method-current) | Returns the current row. | `DataReader` |
| [`getColumnCount()`](#method-getColumnCount) | Returns the number of columns in the result set. | `DataReader` |
| [`getIsClosed()`](#method-getIsClosed) | Whether the reader is closed or not. | `DataReader` |
| [`getRowCount()`](#method-getRowCount) | Returns the number of rows in the result set. | `DataReader` |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`key()`](#method-key) | Returns the index of the current row. | `DataReader` |
| [`next()`](#method-next) | Moves the internal pointer to the next row. | `DataReader` |
| [`nextResult()`](#method-nextResult) | Advances the reader to the next result when reading the results of a batch of statements. | `DataReader` |
| [`read()`](#method-read) | Advances the reader to the next row in a result set. | `DataReader` |
| [`readAll()`](#method-readAll) | Reads the whole result set into an array. | `DataReader` |
| [`readColumn()`](#method-readColumn) | Returns a single column from the next row of a result set. | `DataReader` |
| [`readObject()`](#method-readObject) | Returns an object populated with the next row of data. | `DataReader` |
| [`rewind()`](#method-rewind) | Resets the iterator to the initial state. | `DataReader` |
| [`setFetchMode()`](#method-setFetchMode) | Set the default fetch mode for this statement. | `DataReader` |
| [`valid()`](#method-valid) | Returns whether there is a row of data at current position. | `DataReader` |

## Method details

### __construct() <span id="method-__construct"></span>

```php
public function __construct(yii\db\Command $command, array $config = [])
```

Constructor.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$command` | [`Command`](/docs/api/yii/db/command/index.md) | The command generating the query result |
| `$config` | `array` | Name-value pairs that will be used to initialize the object properties |

### bindColumn() <span id="method-bindColumn"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$column` | `int\|string` | Number 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. |
| `$value` | `mixed` | Name of the PHP variable to which the column will be bound. |
| `$dataType` | `int\|null` | Data type of the parameter |

**Returns**

`mixed`

**See also**

- <https://www.php.net/manual/en/function.PDOStatement-bindColumn.php>

### close() <span id="method-close"></span>

```php
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`

### count() <span id="method-count"></span>

```php
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.

### current() <span id="method-current"></span>

```php
public function current(): mixed
```

Returns the current row.

This method is required by the interface [`Iterator`](https://www.php.net/manual/en/class.iterator.php).

**Returns**

`mixed`: The current row.

### getColumnCount() <span id="method-getColumnCount"></span>

```php
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.

### getIsClosed() <span id="method-getIsClosed"></span>

```php
public function getIsClosed(): bool
```

Whether the reader is closed or not.

**Returns**

`bool`: Whether the reader is closed or not.

### getRowCount() <span id="method-getRowCount"></span>

```php
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.

### key() <span id="method-key"></span>

```php
public function key(): int
```

Returns the index of the current row.

This method is required by the interface [`Iterator`](https://www.php.net/manual/en/class.iterator.php).

**Returns**

`int`: The index of the current row.

### next() <span id="method-next"></span>

```php
public function next(): mixed
```

Moves the internal pointer to the next row.

This method is required by the interface [`Iterator`](https://www.php.net/manual/en/class.iterator.php).

**Returns**

`mixed`

### nextResult() <span id="method-nextResult"></span>

```php
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.

### read() <span id="method-read"></span>

```php
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

### readAll() <span id="method-readAll"></span>

```php
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.

### readColumn() <span id="method-readColumn"></span>

```php
public function readColumn(int $columnIndex): mixed
```

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

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$columnIndex` | `int` | Zero-based column index |

**Returns**

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

### readObject() <span id="method-readObject"></span>

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

Returns an object populated with the next row of data.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$className` | `string` | Class name of the object to be created and populated |
| `$fields` | `array` | Elements of this array are passed to the constructor |

**Returns**

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

### rewind() <span id="method-rewind"></span>

```php
public function rewind(): mixed
```

Resets the iterator to the initial state.

This method is required by the interface [`Iterator`](https://www.php.net/manual/en/class.iterator.php).

**Returns**

`mixed`

**Throws**

- [`yii\base\InvalidCallException`](/docs/api/yii/base/invalidcallexception/index.md): if this method is invoked twice

### setFetchMode() <span id="method-setFetchMode"></span>

```php
public function setFetchMode(int $mode): mixed
```

Set the default fetch mode for this statement.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$mode` | `int` | Fetch mode |

**Returns**

`mixed`

**See also**

- <https://www.php.net/manual/en/function.PDOStatement-setFetchMode.php>

### valid() <span id="method-valid"></span>

```php
public function valid(): bool
```

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

This method is required by the interface [`Iterator`](https://www.php.net/manual/en/class.iterator.php).

**Returns**

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