-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02.execute-select-query.php
96 lines (68 loc) · 2.29 KB
/
02.execute-select-query.php
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/*
* This file is part of the VV package.
*
* (c) Volodymyr Sarnytskyi <v00v4n@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace examples;
require __DIR__ . '/../../bootstrap.php';
use App\Db\MainDb;
use VV\Db;
$db = MainDb::instance();
// Fetch single row or column:
$query = $db->tbl->product->select('product_id', 'title')->whereId(10);
echo "\n# ->result->row: ";
print_r($query->result->row/*($flags)*/);
echo "\n# ->result->cell:\n";
echo $query->result->cell/*($columnIndex[, $flags])*/;
echo "\n";
//
// Fetch all rows:
$query = $db->tbl->product->select('product_id', 'title', 'b.title brand')->join($db->tbl->brand)->limit(3);
echo "\n# while(->result()->fetch()): \n";
$result = $query->result();
while (['product_id' => $productId, 'title' => $title] = $result->fetch()) {
echo "$productId: $title\n";
}
echo "\n# foreach(->result()): \n";
$rowIterator = $query->result(Db::FETCH_NUM);
foreach ($rowIterator as [$productId, $title, $brand]) {
echo "$productId: $brand $title\n";
}
echo "\n# ->result->column: ";
print_r($query->result()->column/*($columnIndex[, $flags])*/);
echo "\n# ->result->rows: ";
print_r($query->result()->rows/*($flags)*/);
echo "\n# ->result->assoc: ";
print_r($query->result()->assoc/*(keyColumn: 'product_id'[, valueColumn: 'title'])*/);
//
// Fetch result directly from query:
$query = $db->tbl->product->select('product_id', 'title', 'brand_id')->limit(3);
echo "\n# ->rows: ";
print_r($query->rows);
echo "\n# ->rows(FETCH_NUM): ";
print_r($query->rows(Db::FETCH_NUM));
echo "\n# ->rows(decorator: fn): ";
print_r($query->rows(decorator: function (&$row, &$key) {
$key = $row['product_id'] . '-' . $row['brand_id'];
$row = array_values($row);
}));
echo "\n# ->rows(key => value): ";
print_r($query->rows(keyColumn: 'product_id', decorator: 'title'));
echo "\n# ->assoc: ";
print_r($query->assoc);
echo "\n# ->row: ";
print_r($query->row);
echo "\n# ->row(FETCH_BOTH): ";
print_r($query->row(Db::FETCH_NUM | Db::FETCH_ASSOC));
echo "\n# ->column: ";
print_r($query->column);
echo "\n# ->column(1): ";
print_r($query->column(1));
echo "\n# ->cell:\n", $query->cell, "\n";
echo "\n# ->cell(1):\n", $query->cell(1), "\n";
//