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