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