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($sql = 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\n", $offset + 1, $link->errno, $link->error);
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\n", $offset + 1, $link->errno, $link->error);
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        $fields = mysqli_fetch_fields($res);
125
126        if (!(gettype($php_value)=="unicode" && ($fields[1]->flags & 128))) {
127            if ($regexp_comparison) {
128                if (!preg_match($regexp_comparison, (string)$row['label']) || !preg_match($regexp_comparison, (string)$row[1])) {
129                    printf("[%04d] Expecting %s/%s [reg exp = %s], got %s/%s resp. %s/%s. [%d] %s\n", $offset + 4,
130                        gettype($php_value), $php_value, $regexp_comparison,
131                        gettype($row[1]), $row[1],
132                        gettype($row['label']), $row['label'], $link->errno, $link->error);
133                    return false;
134                }
135            } else {
136                if (($row['label'] !== $php_value) || ($row[1] != $php_value)) {
137                    printf("[%04d] Expecting %s/%s, got %s/%s resp. %s/%s. [%d] %s\n", $offset + 4,
138                        gettype($php_value), $php_value,
139                        gettype($row[1]), $row[1],
140                        gettype($row['label']), $row['label'], $link->errno, $link->error);
141                    return false;
142                }
143            }
144        }
145
146        return true;
147    }
148
149    function func_mysqli_fetch_array_oo_make_string($len) {
150
151        $ret = '';
152        for ($i = 0; $i < $len; $i++)
153            $ret .= chr(mt_rand(65, 90));
154
155        return $ret;
156    }
157
158    func_mysqli_fetch_all_oo($link, $engine, "TINYINT", -11, "-11", 20);
159    func_mysqli_fetch_all_oo($link, $engine, "TINYINT", NULL, NULL, 30);
160    func_mysqli_fetch_all_oo($link, $engine, "TINYINT UNSIGNED", 1, "1", 40);
161    func_mysqli_fetch_all_oo($link, $engine, "TINYINT UNSIGNED", NULL, NULL, 50);
162
163    func_mysqli_fetch_all_oo($link, $engine, "BOOL", 1, "1", 60);
164    func_mysqli_fetch_all_oo($link, $engine, "BOOL", NULL, NULL, 70);
165    func_mysqli_fetch_all_oo($link, $engine, "BOOLEAN", 0, "0", 80);
166    func_mysqli_fetch_all_oo($link, $engine, "BOOLEAN", NULL, NULL, 90);
167
168    func_mysqli_fetch_all_oo($link, $engine, "SMALLINT", -32768, "-32768", 100);
169    func_mysqli_fetch_all_oo($link, $engine, "SMALLINT", 32767, "32767", 110);
170    func_mysqli_fetch_all_oo($link, $engine, "SMALLINT", NULL, NULL, 120);
171    func_mysqli_fetch_all_oo($link, $engine, "SMALLINT UNSIGNED", 65400, "65400", 130);
172    func_mysqli_fetch_all_oo($link, $engine, "SMALLINT UNSIGNED", NULL, NULL, 140);
173
174    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMINT", -8388608, "-8388608", 150);
175    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMINT", 8388607, "8388607", 160);
176    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMINT", NULL, NULL, 170);
177    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMINT UNSIGNED", 16777215, "16777215", 180);
178    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMINT UNSIGNED", NULL, NULL, 190);
179
180    func_mysqli_fetch_all_oo($link, $engine, "INTEGER", -2147483648, "-2147483648", 200);
181    func_mysqli_fetch_all_oo($link, $engine, "INTEGER", 2147483647, "2147483647", 210);
182    func_mysqli_fetch_all_oo($link, $engine, "INTEGER", NULL, NULL, 220);
183    func_mysqli_fetch_all_oo($link, $engine, "INTEGER UNSIGNED", "4294967295", "4294967295", 230);
184    func_mysqli_fetch_all_oo($link, $engine, "INTEGER UNSIGNED", NULL, NULL, 240);
185
186    func_mysqli_fetch_all_oo($link, $engine, "BIGINT", "-9223372036854775808", "-9223372036854775808", 250);
187    func_mysqli_fetch_all_oo($link, $engine, "BIGINT", NULL, NULL, 260);
188    func_mysqli_fetch_all_oo($link, $engine, "BIGINT UNSIGNED", "18446744073709551615", "18446744073709551615", 270);
189    func_mysqli_fetch_all_oo($link, $engine, "BIGINT UNSIGNED", NULL, NULL, 280);
190
191    func_mysqli_fetch_all_oo($link, $engine, "FLOAT", (string)(-9223372036854775808 - 1.1), "-9.22337e+18", 290, "/-9\.22337e\+?[0]?18/iu");
192    func_mysqli_fetch_all_oo($link, $engine, "FLOAT", NULL, NULL, 300);
193    func_mysqli_fetch_all_oo($link, $engine, "FLOAT UNSIGNED", (string)(18446744073709551615 + 1.1), "1.84467e+?19", 310, "/1\.84467e\+?[0]?19/iu");
194    func_mysqli_fetch_all_oo($link, $engine, "FLOAT UNSIGNED ", NULL, NULL, 320);
195
196    func_mysqli_fetch_all_oo($link, $engine, "DOUBLE(10,2)", "-99999999.99", "-99999999.99", 330);
197    func_mysqli_fetch_all_oo($link, $engine, "DOUBLE(10,2)", NULL, NULL, 340);
198    func_mysqli_fetch_all_oo($link, $engine, "DOUBLE(10,2) UNSIGNED", "99999999.99", "99999999.99", 350);
199    func_mysqli_fetch_all_oo($link, $engine, "DOUBLE(10,2) UNSIGNED", NULL, NULL, 360);
200
201    func_mysqli_fetch_all_oo($link, $engine, "DECIMAL(10,2)", "-99999999.99", "-99999999.99", 370);
202    func_mysqli_fetch_all_oo($link, $engine, "DECIMAL(10,2)", NULL, NULL, 380);
203    func_mysqli_fetch_all_oo($link, $engine, "DECIMAL(10,2)", "99999999.99", "99999999.99", 390);
204    func_mysqli_fetch_all_oo($link, $engine, "DECIMAL(10,2)", NULL, NULL, 400);
205
206    // don't care about date() strict TZ warnings...
207    $date = @date('Y-m-d');
208    func_mysqli_fetch_all_oo($link, $engine, "DATE", $date, $date, 410);
209    func_mysqli_fetch_all_oo($link, $engine, "DATE NOT NULL", $date, $date, 420);
210    func_mysqli_fetch_all_oo($link, $engine, "DATE", NULL, NULL, 430);
211
212    $datetime = @date('Y-m-d H:i:s');
213    func_mysqli_fetch_all_oo($link, $engine, "DATETIME", $datetime, $datetime, 440);
214    func_mysqli_fetch_all_oo($link, $engine, "DATETIME NOT NULL", $datetime, $datetime, 450);
215    func_mysqli_fetch_all_oo($link, $engine, "DATETIME", NULL, NULL, 460);
216
217    func_mysqli_fetch_all_oo($link, $engine, "TIMESTAMP", $datetime, $datetime, 470);
218
219    $time = @date('H:i:s');
220    func_mysqli_fetch_all_oo($link, $engine, "TIME", $time, $time, 480);
221    func_mysqli_fetch_all_oo($link, $engine, "TIME NOT NULL", $time, $time, 490);
222    func_mysqli_fetch_all_oo($link, $engine, "TIME", NULL, NULL, 500);
223
224    func_mysqli_fetch_all_oo($link, $engine, "YEAR", @date('Y'), @date('Y'), 510);
225    func_mysqli_fetch_all_oo($link, $engine, "YEAR NOT NULL", @date('Y'), @date('Y'), 520);
226    func_mysqli_fetch_all_oo($link, $engine, "YEAR", NULL, NULL, 530);
227
228    $string255 = func_mysqli_fetch_array_oo_make_string(255);
229    func_mysqli_fetch_all_oo($link, $engine, "CHAR(1)", "a", "a", 540);
230    func_mysqli_fetch_all_oo($link, $engine, "CHAR(255)", $string255,  $string255, 550);
231    func_mysqli_fetch_all_oo($link, $engine, "CHAR(1) NOT NULL", "a", "a", 560);
232    func_mysqli_fetch_all_oo($link, $engine, "CHAR(1)", NULL, NULL, 570);
233
234    $string65k = func_mysqli_fetch_array_oo_make_string(65400);
235    func_mysqli_fetch_all_oo($link, $engine, "VARCHAR(1)", "a", "a", 580);
236    func_mysqli_fetch_all_oo($link, $engine, "VARCHAR(255)", $string255, $string255, 590);
237    func_mysqli_fetch_all_oo($link, $engine, "VARCHAR(65400)", $string65k, $string65k, 600);
238    func_mysqli_fetch_all_oo($link, $engine, "VARCHAR(1) NOT NULL", "a", "a", 610);
239    func_mysqli_fetch_all_oo($link, $engine, "VARCHAR(1)", NULL, NULL, 620);
240
241    func_mysqli_fetch_all_oo($link, $engine, "BINARY(1)", "a", "a", 630);
242    func_mysqli_fetch_all_oo($link, $engine, "BINARY(2)", chr(0) . "a", chr(0) . "a", 640);
243    func_mysqli_fetch_all_oo($link, $engine, "BINARY(1) NOT NULL", "b", "b", 650);
244    func_mysqli_fetch_all_oo($link, $engine, "BINARY(1)", NULL, NULL, 660);
245
246    func_mysqli_fetch_all_oo($link, $engine, "VARBINARY(1)", "a", "a", 670);
247    func_mysqli_fetch_all_oo($link, $engine, "VARBINARY(2)", chr(0) . "a", chr(0) . "a", 680);
248    func_mysqli_fetch_all_oo($link, $engine, "VARBINARY(1) NOT NULL", "b", "b", 690);
249    func_mysqli_fetch_all_oo($link, $engine, "VARBINARY(1)", NULL, NULL, 700);
250
251    func_mysqli_fetch_all_oo($link, $engine, "TINYBLOB", "a", "a", 710);
252    func_mysqli_fetch_all_oo($link, $engine, "TINYBLOB", chr(0) . "a", chr(0) . "a", 720);
253    func_mysqli_fetch_all_oo($link, $engine, "TINYBLOB NOT NULL", "b", "b", 730);
254    func_mysqli_fetch_all_oo($link, $engine, "TINYBLOB", NULL, NULL, 740);
255
256    func_mysqli_fetch_all_oo($link, $engine, "TINYTEXT", "a", "a", 750);
257    func_mysqli_fetch_all_oo($link, $engine, "TINYTEXT NOT NULL", "a", "a", 760);
258    func_mysqli_fetch_all_oo($link, $engine, "TINYTEXT", NULL, NULL, 770);
259
260    func_mysqli_fetch_all_oo($link, $engine, "BLOB", "a", "a", 780);
261    func_mysqli_fetch_all_oo($link, $engine, "BLOB", chr(0) . "a", chr(0) . "a", 780);
262    func_mysqli_fetch_all_oo($link, $engine, "BLOB", NULL, NULL, 790);
263
264    func_mysqli_fetch_all_oo($link, $engine, "TEXT", "a", "a", 800);
265    func_mysqli_fetch_all_oo($link, $engine, "TEXT", chr(0) . "a", chr(0) . "a", 810);
266    func_mysqli_fetch_all_oo($link, $engine, "TEXT", NULL, NULL, 820);
267
268    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMBLOB", "a", "a", 830);
269    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMBLOB", chr(0) . "a", chr(0) . "a", 840);
270    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMBLOB", NULL, NULL, 850);
271
272    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMTEXT", "a", "a", 860);
273    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMTEXT", chr(0) . "a", chr(0) . "a", 870);
274    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMTEXT", NULL, NULL, 880);
275
276    func_mysqli_fetch_all_oo($link, $engine, "LONGBLOB", "a", "a", 890);
277    func_mysqli_fetch_all_oo($link, $engine, "LONGTEXT", chr(0) . "a", chr(0) . "a", 900);
278    func_mysqli_fetch_all_oo($link, $engine, "LONGBLOB", NULL, NULL, 910);
279
280    func_mysqli_fetch_all_oo($link, $engine, "ENUM('a', 'b')", "a", "a", 920);
281    func_mysqli_fetch_all_oo($link, $engine, "ENUM('a', 'b')", NULL, NULL, 930);
282
283    func_mysqli_fetch_all_oo($link, $engine, "SET('a', 'b')", "a", "a", 940);
284    func_mysqli_fetch_all_oo($link, $engine, "SET('a', 'b')", NULL, NULL, 950);
285
286    mysqli_close($link);
287
288    try {
289        $res->fetch_array(MYSQLI_ASSOC);
290    } catch (Error $exception) {
291        echo $exception->getMessage() . "\n";
292    }
293
294    print "done!";
295?>
296--CLEAN--
297<?php
298    require_once 'clean_table.inc';
299?>
300--EXPECT--
301[005]
302array(2) {
303  [0]=>
304  array(2) {
305    [0]=>
306    string(1) "1"
307    [1]=>
308    string(1) "a"
309  }
310  [1]=>
311  array(2) {
312    [0]=>
313    string(1) "2"
314    [1]=>
315    string(1) "b"
316  }
317}
318[007]
319array(2) {
320  [0]=>
321  array(2) {
322    [0]=>
323    string(1) "1"
324    [1]=>
325    string(1) "a"
326  }
327  [1]=>
328  array(2) {
329    [0]=>
330    string(1) "2"
331    [1]=>
332    string(1) "b"
333  }
334}
335[008]
336array(2) {
337  [0]=>
338  array(4) {
339    [0]=>
340    string(1) "1"
341    ["id"]=>
342    string(1) "1"
343    [1]=>
344    string(1) "a"
345    ["label"]=>
346    string(1) "a"
347  }
348  [1]=>
349  array(4) {
350    [0]=>
351    string(1) "2"
352    ["id"]=>
353    string(1) "2"
354    [1]=>
355    string(1) "b"
356    ["label"]=>
357    string(1) "b"
358  }
359}
360[010]
361array(2) {
362  [0]=>
363  array(2) {
364    ["id"]=>
365    string(1) "1"
366    ["label"]=>
367    string(1) "a"
368  }
369  [1]=>
370  array(2) {
371    ["id"]=>
372    string(1) "2"
373    ["label"]=>
374    string(1) "b"
375  }
376}
377[011]
378NULL
379[013]
380array(2) {
381  [0]=>
382  array(2) {
383    ["id"]=>
384    string(1) "1"
385    ["label"]=>
386    string(1) "a"
387  }
388  [1]=>
389  array(2) {
390    ["id"]=>
391    string(1) "2"
392    ["label"]=>
393    string(1) "b"
394  }
395}
396[016]
397NULL
398[017]
399array(1) {
400  [0]=>
401  array(11) {
402    [0]=>
403    string(1) "1"
404    ["a"]=>
405    string(1) "2"
406    [1]=>
407    string(1) "2"
408    [2]=>
409    string(1) "3"
410    ["c"]=>
411    string(1) "3"
412    [3]=>
413    string(1) "4"
414    ["C"]=>
415    string(1) "4"
416    [4]=>
417    NULL
418    ["d"]=>
419    NULL
420    [5]=>
421    string(1) "1"
422    ["e"]=>
423    string(1) "1"
424  }
425}
426mysqli_result::fetch_all(): Argument #1 ($mode) must be one of MYSQLI_NUM, MYSQLI_ASSOC, or MYSQLI_BOTH
427mysqli_result object is already closed
428done!
429