Skip to content

ResultSet

Alexey Borzov edited this page Sep 8, 2017 · 1 revision

ResultSet class

This class is a wrapper around pgsql result resource. An instance of it is returned by Connection::execute(), Connection::executeParams(), PreparedStatement::execute() if the executed query returned rows. This instance receives the implementation of TypeConverterFactory used by the Connection executing the query and uses it to convert strings in query result to PHP native types.

Usage examples for query results.

Standard interfaces

ResultSet implements Iterator, Countable, and ArrayAccess SPL interfaces which allow, respectively

  • Iteration over rows with foreach ($result as $row)
  • Getting number of returned rows with count($result)
  • Accessing a specific result row with $result[$index]

Additional public methods

  • fetchAll(int|null $mode = null, string|int $keyColumn = null, bool $forceArray = false, bool $group = false): array - Returns an array containing all rows of the result set
    • $mode - Fetch mode, either PGSQL_ASSOC or PGSQL_NUM. If null, mode set with setMode() will be used.
    • $keyColumn - Either a column name or an index (0-based). If given, values of this column will be used as keys in the outer array.
    • $forceArray - Used only with $keyColumn when the query returns exactly two columns. If true the values will be one element arrays with non-key column's values, instead of values directly.
    • $group - If true, the values in the returned array are wrapped in another array. If there are duplicate values in key column, values of other columns will be appended to this array instead of overwriting previous ones.
  • fetchColumn(string|int $fieldIndex): array - Returns an array containing all values from a given column in the result set. $fieldIndex is either a 0-based index or a name of result column.
  • getFieldCount(): int - Returns the number of fields in the result.
  • getFieldNames(): array - Returns the names of fields in the result.
  • setType(string|int $fieldIndex, mixed $type): $this - Sets the type for a result column. Here $fieldIndex is either a 0-based index or a name of result column, $type is either a TypeConverter instance or a type specification for TypeConverterFactory. Note that it is not usually needed to specify result types: they are deduced from database metadata.
  • setMode(int $mode = PGSQL_ASSOC): $this - Sets how the returned rows are indexed. It accepts either PGSQL_ASSOC or PGSQL_NUM constants used by pg_fetch_row().