-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuery2Result.php
More file actions
241 lines (191 loc) · 4.36 KB
/
Query2Result.php
File metadata and controls
241 lines (191 loc) · 4.36 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
* Query2Result is a class with data manipulation methods
*
* @author Adam Zivner <adam.zivner@gmail.com>
* @license http://www.opensource.org/licenses/bsd-license.php New BSD license
* @package Query2
* @link http://query2.0o.cz
*/
class Query2Result implements Iterator, Countable
{
/** @var mysqli_result - returned by mysqli_query() */
public $res;
/** @var boolean - for multiple uses of fetchAll(), fetchPairs(), fetchCol() and fetchAssoc() so
* we don't have to call rewind() manually */
protected $auto_rewind = false;
public function __construct($res)
{
$this->res = $res;
}
public function __destruct()
{
mysqli_free_result($this->res);
}
/** see $auto_rewind */
protected function checkAutoRewind()
{
if ($this->auto_rewind) {
$this->rewind();
}
$this->auto_rewind = true;
}
/**
* Return first (or $col, if given) column of the first line of the result.
*
* @param string $col
* @return false|string
*/
public function fetchOne($col = '')
{
$row = mysqli_fetch_assoc($this->res);
if (!$row) {
return false;
} else {
return strlen($col) ? $row[$col] : reset($row);
}
}
/**
* Fetch first (or $col, if given) column of all result rows.
*
* @param string $col
* @return array
*/
public function fetchCol($col = '')
{
$this->checkAutoRewind();
$ret = array();
while ($c = $this->fetchOne($col)) {
$ret[] = $c;
}
return $ret;
}
/**
* Return associative array of first result row. You can specify which columns
* should be returned. If no columns are give, method returns all columns.
*
* @return false|array
*/
public function fetchRow()
{
$row = mysqli_fetch_assoc($this->res);
if (!$row || func_num_args() == 0) {
return $row;
} else {
$ret = array();
foreach (is_array(func_get_arg(0)) ? func_get_arg(0) : func_get_args() as $col) {
$ret[$col] = $row[$col];
}
return $ret;
}
}
/**
* Fetch all result rows. You can specify which columns should be returned.
* If no columns are give, method returns all columns.
*
* @return array
*/
public function fetchAll()
{
$this->checkAutoRewind();
$args = func_get_args();
if (isset($args[0]) && is_array($args[0])) {
$args = $args[0];
}
$ret = array();
while ($raw_row = mysqli_fetch_assoc($this->res)) {
$final_row = array();
if (count($args) == 0) {
$final_row = $raw_row;
} else {
foreach ($args as $col) {
$final_row[$col] = $raw_row[$col];
}
}
$ret[] = $final_row;
}
return $ret;
}
/**
* Return all rows associated by given column(s). See http://query2.0o.cz for details.
*
* @return array
*/
public function fetchAssoc()
{
$this->checkAutoRewind();
$cols = func_get_args();
$arr = $this->fetchAll();
$ret = array();
foreach ($arr as $row) {
$cur =& $ret;
foreach ($cols as $col) {
if (!isset($cur[$row[$col]])) {
$cur[$row[$col]] = array();
}
$cur =& $cur[$row[$col]];
}
$cur[] = $row;
}
return $ret;
}
/**
* Return array("id" => "jmeno", ...) consisting of the first two columns
* of all result rows. Useful e.g. for fetching values for select box.
*
* @param string|integer $key - which column should be used for key
* @param string|integer $value - which column should be used for value
* @return array
*/
public function fetchPairs($key = 0, $value = 1)
{
$this->checkAutoRewind();
$ret = array();
while ($row = mysqli_fetch_array($this->res)) {
$ret[$row[$key]] = $row[$value];
}
return $ret;
}
public function numRows()
{
return mysqli_num_rows($this->res);
}
// Countable interface
public function count()
{
return $this->numRows();
}
// Iterator interface
private $idx = 0; // generate some (for each row unique) key
private $row; // current row
/**
* Rewind the result set to the start (or to the given row). It's both
* directly callable and implementation of Iterator interface
*
* @param integer $row - to which row result set should be rewinded
*/
public function rewind($row = 0)
{
if (mysqli_num_rows($this->res)) {
mysqli_data_seek($this->res, 0);
}
$this->idx = 0;
}
public function current()
{
return $this->row;
}
public function key()
{
return $this->idx;
}
public function next()
{
$this->idx++;
}
public function valid()
{
$this->row = $this->fetchRow();
return $this->row ? true : false;
}
}