/
result.texy
52 lines (35 loc) · 1.47 KB
/
result.texy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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.
/--php
$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 hold the data of specific fetched result-row. You can access data by property access with a column name. Use `getNthField()` method to retrieve a column by its numeric index.
/--php
$row = $connection->query('SELECT name, age FROM ...')->fetch();
echo $row->name;
echo $row->age;
echo $row->getNthField(0); // prints name
\--