1--TEST--
2$mysqli->fetch_all() (introduced with mysqlnd)
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once 'skipifconnectfailure.inc';
8?>
9--FILE--
10<?php
11    require 'table.inc';
12    $mysqli = $link;
13    if (!$res = $mysqli->query("SELECT * FROM test ORDER BY id LIMIT 2")) {
14        printf("[004] [%d] %s\n", $mysqli->errno, $mysqli->error);
15    }
16
17    print "[005]\n";
18    var_dump($res->fetch_all());
19    $res->free_result();
20
21    if (!$res = $mysqli->query("SELECT * FROM test ORDER BY id LIMIT 2")) {
22        printf("[006] [%d] %s\n", $mysqli->errno, $mysqli->error);
23    }
24
25    print "[007]\n";
26    var_dump($res->fetch_all(MYSQLI_NUM));
27    $res->free_result();
28
29    if (!$res = $mysqli->query("SELECT * FROM test ORDER BY id LIMIT 2")) {
30        printf("[008] [%d] %s\n", $mysqli->errno, $mysqli->error);
31    }
32
33    print "[008]\n";
34    var_dump($res->fetch_all(MYSQLI_BOTH));
35    $res->free_result();
36
37    if (!$res = $mysqli->query("SELECT * FROM test ORDER BY id LIMIT 2")) {
38        printf("[009] [%d] %s\n", $mysqli->errno, $mysqli->error);
39    }
40
41    print "[010]\n";
42    var_dump($res->fetch_all(MYSQLI_ASSOC));
43
44    print "[011]\n";
45    var_dump($res->fetch_array());
46    $res->free_result();
47
48    if (!$res = $mysqli->query("SELECT * FROM test ORDER BY id LIMIT 2")) {
49        printf("[012] [%d] %s\n", $mysqli->errno, $mysqli->error);
50    }
51
52    print "[013]\n";
53    var_dump($res->fetch_all(MYSQLI_ASSOC));
54
55    print "[016]\n";
56    var_dump($res->fetch_array());
57
58    if (!$res = $mysqli->query("SELECT 1 AS a, 2 AS a, 3 AS c, 4 AS C, NULL AS d, true AS e")) {
59        printf("[010] Cannot run query, [%d] %s\n", $mysqli->errno, $$mysqli->error);
60    }
61    print "[017]\n";
62    var_dump($res->fetch_all(MYSQLI_BOTH));
63
64    $res->free_result();
65    if (!$res = $mysqli->query("SELECT 1 AS a, 2 AS b, 3 AS c, 4 AS C")) {
66        printf("[018] Cannot run query, [%d] %s\n",
67            $mysqli->errno, $$mysqli->error);
68        exit(1);
69    }
70
71    try {
72        $tmp = $res->fetch_all(-10);
73        if (false !== $tmp)
74            printf("[019] Expecting boolean/false although, got %s/%s. [%d] %s\n",
75                gettype($tmp), $tmp, $mysqli->errno, $mysqli->error);
76    } catch (\ValueError $e) {
77        echo $e->getMessage() . \PHP_EOL;
78    }
79
80    $res->free_result();
81
82    function func_mysqli_fetch_all_oo($link, $engine, $sql_type, $sql_value, $php_value, $offset, $regexp_comparison = NULL) {
83
84        if (!$link->query("DROP TABLE IF EXISTS test")) {
85            printf("[%04d] [%d] %s\n", $offset, $link->errno, $link->error);
86            return false;
87        }
88
89        if (!$link->query(sprintf("CREATE TABLE test(id INT NOT NULL, label %s, PRIMARY KEY(id)) ENGINE = %s", $sql_type, $engine))) {
90                // don't bail, engine might not support the datatype
91                return false;
92        }
93
94        if (is_null($php_value)) {
95            if (!$link->query($sql = sprintf("INSERT INTO test(id, label) VALUES (1, NULL)"))) {
96                printf("[%04d] [%d] %s - %s\n", $offset + 1, $link->errno, $link->error, $sql);
97                return false;
98            }
99        } else {
100            if (is_string($sql_value)) {
101                if (!$link->query($sql = "INSERT INTO test(id, label) VALUES (1, '" . $sql_value . "')")) {
102                    printf("[%04ds] [%d] %s - %s\n", $offset + 1, $link->errno, $link->error, $sql);
103                    return false;
104                }
105            } else {
106                if (!$link->query($sql = sprintf("INSERT INTO test(id, label) VALUES (1, '%d')", $sql_value))) {
107                    printf("[%04di] [%d] %s - %s\n", $offset + 1, $link->errno, $link->error, $sql);
108                    return false;
109                }
110            }
111        }
112
113        if (!$res = $link->query("SELECT id, label FROM test")) {
114            printf("[%04d] [%d] %s\n", $offset + 2, $link->errno, $link->error);
115            return false;
116        }
117
118        if (!$tmp = $res->fetch_all(MYSQLI_BOTH)) {
119            printf("[%04d] [%d] %s\n", $offset + 3, $link->errno, $link->error);
120            return false;
121        }
122        $row = $tmp[0];
123
124        if ($regexp_comparison) {
125            if (!preg_match($regexp_comparison, (string)$row['label']) || !preg_match($regexp_comparison, (string)$row[1])) {
126                printf("[%04d] Expecting %s/%s [reg exp = %s], got %s/%s resp. %s/%s. [%d] %s\n", $offset + 4,
127                    gettype($php_value), $php_value, $regexp_comparison,
128                    gettype($row[1]), $row[1],
129                    gettype($row['label']), $row['label'], $link->errno, $link->error);
130                return false;
131            }
132        } else {
133            if (($row['label'] !== $php_value) || ($row[1] != $php_value)) {
134                printf("[%04d] Expecting %s/%s, got %s/%s resp. %s/%s. [%d] %s\n", $offset + 4,
135                    gettype($php_value), $php_value,
136                    gettype($row[1]), $row[1],
137                    gettype($row['label']), $row['label'], $link->errno, $link->error);
138                return false;
139            }
140        }
141
142        return true;
143    }
144
145    function func_mysqli_fetch_array_oo_make_string($len) {
146
147        $ret = '';
148        for ($i = 0; $i < $len; $i++)
149            $ret .= chr(mt_rand(65, 90));
150
151        return $ret;
152    }
153
154    func_mysqli_fetch_all_oo($link, $engine, "TINYINT", -11, "-11", 20);
155    func_mysqli_fetch_all_oo($link, $engine, "TINYINT", NULL, NULL, 30);
156    func_mysqli_fetch_all_oo($link, $engine, "TINYINT UNSIGNED", 1, "1", 40);
157    func_mysqli_fetch_all_oo($link, $engine, "TINYINT UNSIGNED", NULL, NULL, 50);
158
159    func_mysqli_fetch_all_oo($link, $engine, "BOOL", 1, "1", 60);
160    func_mysqli_fetch_all_oo($link, $engine, "BOOL", NULL, NULL, 70);
161    func_mysqli_fetch_all_oo($link, $engine, "BOOLEAN", 0, "0", 80);
162    func_mysqli_fetch_all_oo($link, $engine, "BOOLEAN", NULL, NULL, 90);
163
164    func_mysqli_fetch_all_oo($link, $engine, "SMALLINT", -32768, "-32768", 100);
165    func_mysqli_fetch_all_oo($link, $engine, "SMALLINT", 32767, "32767", 110);
166    func_mysqli_fetch_all_oo($link, $engine, "SMALLINT", NULL, NULL, 120);
167    func_mysqli_fetch_all_oo($link, $engine, "SMALLINT UNSIGNED", 65400, "65400", 130);
168    func_mysqli_fetch_all_oo($link, $engine, "SMALLINT UNSIGNED", NULL, NULL, 140);
169
170    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMINT", -8388608, "-8388608", 150);
171    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMINT", 8388607, "8388607", 160);
172    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMINT", NULL, NULL, 170);
173    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMINT UNSIGNED", 16777215, "16777215", 180);
174    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMINT UNSIGNED", NULL, NULL, 190);
175
176    func_mysqli_fetch_all_oo($link, $engine, "INTEGER", -2147483648, "-2147483648", 200);
177    func_mysqli_fetch_all_oo($link, $engine, "INTEGER", 2147483647, "2147483647", 210);
178    func_mysqli_fetch_all_oo($link, $engine, "INTEGER", NULL, NULL, 220);
179    func_mysqli_fetch_all_oo($link, $engine, "INTEGER UNSIGNED", "4294967295", "4294967295", 230);
180    func_mysqli_fetch_all_oo($link, $engine, "INTEGER UNSIGNED", NULL, NULL, 240);
181
182    func_mysqli_fetch_all_oo($link, $engine, "BIGINT", "-9223372036854775808", "-9223372036854775808", 250);
183    func_mysqli_fetch_all_oo($link, $engine, "BIGINT", NULL, NULL, 260);
184    func_mysqli_fetch_all_oo($link, $engine, "BIGINT UNSIGNED", "18446744073709551615", "18446744073709551615", 270);
185    func_mysqli_fetch_all_oo($link, $engine, "BIGINT UNSIGNED", NULL, NULL, 280);
186
187    func_mysqli_fetch_all_oo($link, $engine, "FLOAT", (string)(-9223372036854775808 - 1.1), "-9.22337e+18", 290, "/-9\.22337e\+?[0]?18/iu");
188    func_mysqli_fetch_all_oo($link, $engine, "FLOAT", NULL, NULL, 300);
189    func_mysqli_fetch_all_oo($link, $engine, "FLOAT UNSIGNED", (string)(18446744073709551615 + 1.1), "1.84467e+?19", 310, "/1\.84467e\+?[0]?19/iu");
190    func_mysqli_fetch_all_oo($link, $engine, "FLOAT UNSIGNED ", NULL, NULL, 320);
191
192    func_mysqli_fetch_all_oo($link, $engine, "DOUBLE(10,2)", "-99999999.99", "-99999999.99", 330);
193    func_mysqli_fetch_all_oo($link, $engine, "DOUBLE(10,2)", NULL, NULL, 340);
194    func_mysqli_fetch_all_oo($link, $engine, "DOUBLE(10,2) UNSIGNED", "99999999.99", "99999999.99", 350);
195    func_mysqli_fetch_all_oo($link, $engine, "DOUBLE(10,2) UNSIGNED", NULL, NULL, 360);
196
197    func_mysqli_fetch_all_oo($link, $engine, "DECIMAL(10,2)", "-99999999.99", "-99999999.99", 370);
198    func_mysqli_fetch_all_oo($link, $engine, "DECIMAL(10,2)", NULL, NULL, 380);
199    func_mysqli_fetch_all_oo($link, $engine, "DECIMAL(10,2)", "99999999.99", "99999999.99", 390);
200    func_mysqli_fetch_all_oo($link, $engine, "DECIMAL(10,2)", NULL, NULL, 400);
201
202    // don't care about date() strict TZ warnings...
203    $date = @date('Y-m-d');
204    func_mysqli_fetch_all_oo($link, $engine, "DATE", $date, $date, 410);
205    func_mysqli_fetch_all_oo($link, $engine, "DATE NOT NULL", $date, $date, 420);
206    func_mysqli_fetch_all_oo($link, $engine, "DATE", NULL, NULL, 430);
207
208    $datetime = @date('Y-m-d H:i:s');
209    func_mysqli_fetch_all_oo($link, $engine, "DATETIME", $datetime, $datetime, 440);
210    func_mysqli_fetch_all_oo($link, $engine, "DATETIME NOT NULL", $datetime, $datetime, 450);
211    func_mysqli_fetch_all_oo($link, $engine, "DATETIME", NULL, NULL, 460);
212
213    func_mysqli_fetch_all_oo($link, $engine, "TIMESTAMP", $datetime, $datetime, 470);
214
215    $time = @date('H:i:s');
216    func_mysqli_fetch_all_oo($link, $engine, "TIME", $time, $time, 480);
217    func_mysqli_fetch_all_oo($link, $engine, "TIME NOT NULL", $time, $time, 490);
218    func_mysqli_fetch_all_oo($link, $engine, "TIME", NULL, NULL, 500);
219
220    func_mysqli_fetch_all_oo($link, $engine, "YEAR", @date('Y'), @date('Y'), 510);
221    func_mysqli_fetch_all_oo($link, $engine, "YEAR NOT NULL", @date('Y'), @date('Y'), 520);
222    func_mysqli_fetch_all_oo($link, $engine, "YEAR", NULL, NULL, 530);
223
224    $string255 = func_mysqli_fetch_array_oo_make_string(255);
225    func_mysqli_fetch_all_oo($link, $engine, "CHAR(1)", "a", "a", 540);
226    func_mysqli_fetch_all_oo($link, $engine, "CHAR(255)", $string255,  $string255, 550);
227    func_mysqli_fetch_all_oo($link, $engine, "CHAR(1) NOT NULL", "a", "a", 560);
228    func_mysqli_fetch_all_oo($link, $engine, "CHAR(1)", NULL, NULL, 570);
229
230    $string65k = func_mysqli_fetch_array_oo_make_string(65400);
231    func_mysqli_fetch_all_oo($link, $engine, "VARCHAR(1)", "a", "a", 580);
232    func_mysqli_fetch_all_oo($link, $engine, "VARCHAR(255)", $string255, $string255, 590);
233    func_mysqli_fetch_all_oo($link, $engine, "VARCHAR(65400)", $string65k, $string65k, 600);
234    func_mysqli_fetch_all_oo($link, $engine, "VARCHAR(1) NOT NULL", "a", "a", 610);
235    func_mysqli_fetch_all_oo($link, $engine, "VARCHAR(1)", NULL, NULL, 620);
236
237    func_mysqli_fetch_all_oo($link, $engine, "BINARY(1)", "a", "a", 630);
238    func_mysqli_fetch_all_oo($link, $engine, "BINARY(2)", chr(0) . "a", chr(0) . "a", 640);
239    func_mysqli_fetch_all_oo($link, $engine, "BINARY(1) NOT NULL", "b", "b", 650);
240    func_mysqli_fetch_all_oo($link, $engine, "BINARY(1)", NULL, NULL, 660);
241
242    func_mysqli_fetch_all_oo($link, $engine, "VARBINARY(1)", "a", "a", 670);
243    func_mysqli_fetch_all_oo($link, $engine, "VARBINARY(2)", chr(0) . "a", chr(0) . "a", 680);
244    func_mysqli_fetch_all_oo($link, $engine, "VARBINARY(1) NOT NULL", "b", "b", 690);
245    func_mysqli_fetch_all_oo($link, $engine, "VARBINARY(1)", NULL, NULL, 700);
246
247    func_mysqli_fetch_all_oo($link, $engine, "TINYBLOB", "a", "a", 710);
248    func_mysqli_fetch_all_oo($link, $engine, "TINYBLOB", chr(0) . "a", chr(0) . "a", 720);
249    func_mysqli_fetch_all_oo($link, $engine, "TINYBLOB NOT NULL", "b", "b", 730);
250    func_mysqli_fetch_all_oo($link, $engine, "TINYBLOB", NULL, NULL, 740);
251
252    func_mysqli_fetch_all_oo($link, $engine, "TINYTEXT", "a", "a", 750);
253    func_mysqli_fetch_all_oo($link, $engine, "TINYTEXT NOT NULL", "a", "a", 760);
254    func_mysqli_fetch_all_oo($link, $engine, "TINYTEXT", NULL, NULL, 770);
255
256    func_mysqli_fetch_all_oo($link, $engine, "BLOB", "a", "a", 780);
257    func_mysqli_fetch_all_oo($link, $engine, "BLOB", chr(0) . "a", chr(0) . "a", 780);
258    func_mysqli_fetch_all_oo($link, $engine, "BLOB", NULL, NULL, 790);
259
260    func_mysqli_fetch_all_oo($link, $engine, "TEXT", "a", "a", 800);
261    func_mysqli_fetch_all_oo($link, $engine, "TEXT", chr(0) . "a", chr(0) . "a", 810);
262    func_mysqli_fetch_all_oo($link, $engine, "TEXT", NULL, NULL, 820);
263
264    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMBLOB", "a", "a", 830);
265    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMBLOB", chr(0) . "a", chr(0) . "a", 840);
266    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMBLOB", NULL, NULL, 850);
267
268    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMTEXT", "a", "a", 860);
269    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMTEXT", chr(0) . "a", chr(0) . "a", 870);
270    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMTEXT", NULL, NULL, 880);
271
272    func_mysqli_fetch_all_oo($link, $engine, "LONGBLOB", "a", "a", 890);
273    func_mysqli_fetch_all_oo($link, $engine, "LONGTEXT", chr(0) . "a", chr(0) . "a", 900);
274    func_mysqli_fetch_all_oo($link, $engine, "LONGBLOB", NULL, NULL, 910);
275
276    func_mysqli_fetch_all_oo($link, $engine, "ENUM('a', 'b')", "a", "a", 920);
277    func_mysqli_fetch_all_oo($link, $engine, "ENUM('a', 'b')", NULL, NULL, 930);
278
279    func_mysqli_fetch_all_oo($link, $engine, "SET('a', 'b')", "a", "a", 940);
280    func_mysqli_fetch_all_oo($link, $engine, "SET('a', 'b')", NULL, NULL, 950);
281
282    mysqli_close($link);
283
284    try {
285        $res->fetch_array(MYSQLI_ASSOC);
286    } catch (Error $exception) {
287        echo $exception->getMessage() . "\n";
288    }
289
290    print "done!";
291?>
292--CLEAN--
293<?php
294    require_once 'clean_table.inc';
295?>
296--EXPECT--
297[005]
298array(2) {
299  [0]=>
300  array(2) {
301    [0]=>
302    string(1) "1"
303    [1]=>
304    string(1) "a"
305  }
306  [1]=>
307  array(2) {
308    [0]=>
309    string(1) "2"
310    [1]=>
311    string(1) "b"
312  }
313}
314[007]
315array(2) {
316  [0]=>
317  array(2) {
318    [0]=>
319    string(1) "1"
320    [1]=>
321    string(1) "a"
322  }
323  [1]=>
324  array(2) {
325    [0]=>
326    string(1) "2"
327    [1]=>
328    string(1) "b"
329  }
330}
331[008]
332array(2) {
333  [0]=>
334  array(4) {
335    [0]=>
336    string(1) "1"
337    ["id"]=>
338    string(1) "1"
339    [1]=>
340    string(1) "a"
341    ["label"]=>
342    string(1) "a"
343  }
344  [1]=>
345  array(4) {
346    [0]=>
347    string(1) "2"
348    ["id"]=>
349    string(1) "2"
350    [1]=>
351    string(1) "b"
352    ["label"]=>
353    string(1) "b"
354  }
355}
356[010]
357array(2) {
358  [0]=>
359  array(2) {
360    ["id"]=>
361    string(1) "1"
362    ["label"]=>
363    string(1) "a"
364  }
365  [1]=>
366  array(2) {
367    ["id"]=>
368    string(1) "2"
369    ["label"]=>
370    string(1) "b"
371  }
372}
373[011]
374NULL
375[013]
376array(2) {
377  [0]=>
378  array(2) {
379    ["id"]=>
380    string(1) "1"
381    ["label"]=>
382    string(1) "a"
383  }
384  [1]=>
385  array(2) {
386    ["id"]=>
387    string(1) "2"
388    ["label"]=>
389    string(1) "b"
390  }
391}
392[016]
393NULL
394[017]
395array(1) {
396  [0]=>
397  array(11) {
398    [0]=>
399    string(1) "1"
400    ["a"]=>
401    string(1) "2"
402    [1]=>
403    string(1) "2"
404    [2]=>
405    string(1) "3"
406    ["c"]=>
407    string(1) "3"
408    [3]=>
409    string(1) "4"
410    ["C"]=>
411    string(1) "4"
412    [4]=>
413    NULL
414    ["d"]=>
415    NULL
416    [5]=>
417    string(1) "1"
418    ["e"]=>
419    string(1) "1"
420  }
421}
422mysqli_result::fetch_all(): Argument #1 ($mode) must be one of MYSQLI_NUM, MYSQLI_ASSOC, or MYSQLI_BOTH
423mysqli_result object is already closed
424done!
425