Fork me on GitHub

Result

Connection::query() method returns a Nextras\Dbal\Result\Result instance. You can call fetching methods to get the fetched data.

  • Result::fetchAll() returns an array of Nextras\Dbal\Result\Row instances.
  • Result::fetch() returns the next unfetched Row instance.
  • Result::fetchField($column) returns nth column of the next unfetched Row instance.
  • Result::fetchPairs($key, $value) transforms all fetched Row instances into an associative array, see examples below.
$result = $connection->query('SELECT ...');
foreach ($result as $row) { // equals to $result->fetchAll() as $row
}


$result = $connection->query('SELECT ...');
$row = $result->fetch();


$result = $connection->query('SELECT name FROM ...');
$name = $result->fetchField();


$result = $connection->query('SELECT name, age FROM ...');
$assoc = $result->fetchPairs('name', 'age');
// ['peter' => 20, 'john' => 13]

$assoc = $result->fetchPairs(null, 'age');
// [20, 13]

$assoc = $result->fetchPairs('name', null);
// [
//  'peter' => new Row(['name' => 'peter', 'age' => 20]),
//  'john' => new Row(['name' => 'john', 'age' => 13]),
// ]

Row#

Row instances holds the data of specific fetched result-row. You can access data by property access with a column name. Use getNthField() method to retrieve column by numeric index.

$row = $connection->query('SELECT name, age FROM ...')->fetch();

echo $row->name;
echo $row->age;

echo $row->getNthField(0); // prints name