1--TEST--
2$mysqli->fetch_all() (introduced with mysqlnd)
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
7require_once('skipifconnectfailure.inc');
8
9if (!function_exists('mysqli_fetch_all'))
10    die("skip: function only available with mysqlnd");
11?>
12--FILE--
13<?php
14    require_once("connect.inc");
15
16    $tmp    = NULL;
17    $link   = NULL;
18
19    $mysqli = new mysqli();
20
21    if (!$mysqli = new my_mysqli($host, $user, $passwd, $db, $port, $socket))
22        printf("[002] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
23            $host, $user, $db, $port, $socket);
24
25    require('table.inc');
26    if (!$res = $mysqli->query("SELECT * FROM test ORDER BY id LIMIT 2")) {
27        printf("[004] [%d] %s\n", $mysqli->errno, $mysqli->error);
28    }
29
30    print "[005]\n";
31    var_dump($res->fetch_all());
32    $res->free_result();
33
34    if (!$res = $mysqli->query("SELECT * FROM test ORDER BY id LIMIT 2")) {
35        printf("[006] [%d] %s\n", $mysqli->errno, $mysqli->error);
36    }
37
38    print "[007]\n";
39    var_dump($res->fetch_all(MYSQLI_NUM));
40    $res->free_result();
41
42    if (!$res = $mysqli->query("SELECT * FROM test ORDER BY id LIMIT 2")) {
43        printf("[008] [%d] %s\n", $mysqli->errno, $mysqli->error);
44    }
45
46    print "[008]\n";
47    var_dump($res->fetch_all(MYSQLI_BOTH));
48    $res->free_result();
49
50    if (!$res = $mysqli->query("SELECT * FROM test ORDER BY id LIMIT 2")) {
51        printf("[009] [%d] %s\n", $mysqli->errno, $mysqli->error);
52    }
53
54    print "[010]\n";
55    var_dump($res->fetch_all(MYSQLI_ASSOC));
56
57    print "[011]\n";
58    var_dump($res->fetch_array());
59    $res->free_result();
60
61    if (!$res = $mysqli->query("SELECT * FROM test ORDER BY id LIMIT 2")) {
62        printf("[012] [%d] %s\n", $mysqli->errno, $mysqli->error);
63    }
64
65    print "[013]\n";
66    var_dump($res->fetch_all(MYSQLI_ASSOC));
67
68    print "[016]\n";
69    var_dump($res->fetch_array());
70
71    if (!$res = $mysqli->query("SELECT 1 AS a, 2 AS a, 3 AS c, 4 AS C, NULL AS d, true AS e")) {
72        printf("[010] Cannot run query, [%d] %s\n", $mysqli->errno, $$mysqli->error);
73    }
74    print "[017]\n";
75    var_dump($res->fetch_all(MYSQLI_BOTH));
76
77    $res->free_result();
78    if (!$res = $mysqli->query("SELECT 1 AS a, 2 AS b, 3 AS c, 4 AS C")) {
79        printf("[018] Cannot run query, [%d] %s\n",
80            $mysqli->errno, $$mysqli->error);
81        exit(1);
82    }
83
84    do {
85        $illegal_mode = mt_rand(-10000, 10000);
86    } while (in_array($illegal_mode, array(MYSQLI_ASSOC, MYSQLI_NUM, MYSQLI_BOTH)));
87    // NOTE: for BC reasons with ext/mysql, ext/mysqli accepts invalid result modes.
88    $tmp = $res->fetch_all($illegal_mode);
89    if (false !== $tmp)
90        printf("[019] Expecting boolean/false although, got %s/%s. [%d] %s\n",
91            gettype($tmp), $tmp, $mysqli->errno, $mysqli->error);
92
93    $res->free_result();
94
95    function func_mysqli_fetch_all_oo($link, $engine, $sql_type, $sql_value, $php_value, $offset, $regexp_comparison = NULL) {
96
97        if (!$link->query("DROP TABLE IF EXISTS test")) {
98            printf("[%04d] [%d] %s\n", $offset, $link->errno, $link->error);
99            return false;
100        }
101
102        if (!$link->query($sql = sprintf("CREATE TABLE test(id INT NOT NULL, label %s, PRIMARY KEY(id)) ENGINE = %s", $sql_type, $engine))) {
103                // don't bail, engine might not support the datatype
104                return false;
105        }
106
107        if (is_null($php_value)) {
108            if (!$link->query($sql = sprintf("INSERT INTO test(id, label) VALUES (1, NULL)"))) {
109                printf("[%04d] [%d] %s\n", $offset + 1, $link->errno, $link->error);
110                return false;
111            }
112        } else {
113            if (is_string($sql_value)) {
114                if (!$link->query($sql = "INSERT INTO test(id, label) VALUES (1, '" . $sql_value . "')")) {
115                    printf("[%04ds] [%d] %s - %s\n", $offset + 1, $link->errno, $link->error, $sql);
116                    return false;
117                }
118            } else {
119                if (!$link->query($sql = sprintf("INSERT INTO test(id, label) VALUES (1, '%d')", $sql_value))) {
120                    printf("[%04di] [%d] %s\n", $offset + 1, $link->errno, $link->error);
121                    return false;
122                }
123            }
124        }
125
126        if (!$res = $link->query("SELECT id, label FROM test")) {
127            printf("[%04d] [%d] %s\n", $offset + 2, $link->errno, $link->error);
128            return false;
129        }
130
131        if (!$tmp = $res->fetch_all(MYSQLI_BOTH)) {
132            printf("[%04d] [%d] %s\n", $offset + 3, $link->errno, $link->error);
133            return false;
134        }
135        $row = $tmp[0];
136
137        $fields = mysqli_fetch_fields($res);
138
139        if (!(gettype($php_value)=="unicode" && ($fields[1]->flags & 128))) {
140            if ($regexp_comparison) {
141                if (!preg_match($regexp_comparison, (string)$row['label']) || !preg_match($regexp_comparison, (string)$row[1])) {
142                    printf("[%04d] Expecting %s/%s [reg exp = %s], got %s/%s resp. %s/%s. [%d] %s\n", $offset + 4,
143                        gettype($php_value), $php_value, $regexp_comparison,
144                        gettype($row[1]), $row[1],
145                        gettype($row['label']), $row['label'], $link->errno, $link->error);
146                    return false;
147                }
148            } else {
149                if (($row['label'] !== $php_value) || ($row[1] != $php_value)) {
150                    printf("[%04d] Expecting %s/%s, got %s/%s resp. %s/%s. [%d] %s\n", $offset + 4,
151                        gettype($php_value), $php_value,
152                        gettype($row[1]), $row[1],
153                        gettype($row['label']), $row['label'], $link->errno, $link->error);
154                    return false;
155                }
156            }
157        }
158
159        return true;
160    }
161
162    function func_mysqli_fetch_array_oo_make_string($len) {
163
164        $ret = '';
165        for ($i = 0; $i < $len; $i++)
166            $ret .= chr(mt_rand(65, 90));
167
168        return $ret;
169    }
170
171    func_mysqli_fetch_all_oo($link, $engine, "TINYINT", -11, "-11", 20);
172    func_mysqli_fetch_all_oo($link, $engine, "TINYINT", NULL, NULL, 30);
173    func_mysqli_fetch_all_oo($link, $engine, "TINYINT UNSIGNED", 1, "1", 40);
174    func_mysqli_fetch_all_oo($link, $engine, "TINYINT UNSIGNED", NULL, NULL, 50);
175
176    func_mysqli_fetch_all_oo($link, $engine, "BOOL", 1, "1", 60);
177    func_mysqli_fetch_all_oo($link, $engine, "BOOL", NULL, NULL, 70);
178    func_mysqli_fetch_all_oo($link, $engine, "BOOLEAN", 0, "0", 80);
179    func_mysqli_fetch_all_oo($link, $engine, "BOOLEAN", NULL, NULL, 90);
180
181    func_mysqli_fetch_all_oo($link, $engine, "SMALLINT", -32768, "-32768", 100);
182    func_mysqli_fetch_all_oo($link, $engine, "SMALLINT", 32767, "32767", 110);
183    func_mysqli_fetch_all_oo($link, $engine, "SMALLINT", NULL, NULL, 120);
184    func_mysqli_fetch_all_oo($link, $engine, "SMALLINT UNSIGNED", 65400, "65400", 130);
185    func_mysqli_fetch_all_oo($link, $engine, "SMALLINT UNSIGNED", NULL, NULL, 140);
186
187    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMINT", -8388608, "-8388608", 150);
188    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMINT", 8388607, "8388607", 160);
189    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMINT", NULL, NULL, 170);
190    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMINT UNSIGNED", 16777215, "16777215", 180);
191    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMINT UNSIGNED", NULL, NULL, 190);
192
193    func_mysqli_fetch_all_oo($link, $engine, "INTEGER", -2147483648, "-2147483648", 200);
194    func_mysqli_fetch_all_oo($link, $engine, "INTEGER", 2147483647, "2147483647", 210);
195    func_mysqli_fetch_all_oo($link, $engine, "INTEGER", NULL, NULL, 220);
196    func_mysqli_fetch_all_oo($link, $engine, "INTEGER UNSIGNED", "4294967295", "4294967295", 230);
197    func_mysqli_fetch_all_oo($link, $engine, "INTEGER UNSIGNED", NULL, NULL, 240);
198
199    func_mysqli_fetch_all_oo($link, $engine, "BIGINT", "-9223372036854775808", "-9223372036854775808", 250);
200    func_mysqli_fetch_all_oo($link, $engine, "BIGINT", NULL, NULL, 260);
201    func_mysqli_fetch_all_oo($link, $engine, "BIGINT UNSIGNED", "18446744073709551615", "18446744073709551615", 270);
202    func_mysqli_fetch_all_oo($link, $engine, "BIGINT UNSIGNED", NULL, NULL, 280);
203
204    func_mysqli_fetch_all_oo($link, $engine, "FLOAT", (string)(-9223372036854775808 - 1.1), "-9.22337e+18", 290, "/-9\.22337e\+?[0]?18/iu");
205    func_mysqli_fetch_all_oo($link, $engine, "FLOAT", NULL, NULL, 300);
206    func_mysqli_fetch_all_oo($link, $engine, "FLOAT UNSIGNED", (string)(18446744073709551615 + 1.1), "1.84467e+?19", 310, "/1\.84467e\+?[0]?19/iu");
207    func_mysqli_fetch_all_oo($link, $engine, "FLOAT UNSIGNED ", NULL, NULL, 320);
208
209    func_mysqli_fetch_all_oo($link, $engine, "DOUBLE(10,2)", "-99999999.99", "-99999999.99", 330);
210    func_mysqli_fetch_all_oo($link, $engine, "DOUBLE(10,2)", NULL, NULL, 340);
211    func_mysqli_fetch_all_oo($link, $engine, "DOUBLE(10,2) UNSIGNED", "99999999.99", "99999999.99", 350);
212    func_mysqli_fetch_all_oo($link, $engine, "DOUBLE(10,2) UNSIGNED", NULL, NULL, 360);
213
214    func_mysqli_fetch_all_oo($link, $engine, "DECIMAL(10,2)", "-99999999.99", "-99999999.99", 370);
215    func_mysqli_fetch_all_oo($link, $engine, "DECIMAL(10,2)", NULL, NULL, 380);
216    func_mysqli_fetch_all_oo($link, $engine, "DECIMAL(10,2)", "99999999.99", "99999999.99", 390);
217    func_mysqli_fetch_all_oo($link, $engine, "DECIMAL(10,2)", NULL, NULL, 400);
218
219    // don't care about date() strict TZ warnings...
220    $date = @date('Y-m-d');
221    func_mysqli_fetch_all_oo($link, $engine, "DATE", $date, $date, 410);
222    func_mysqli_fetch_all_oo($link, $engine, "DATE NOT NULL", $date, $date, 420);
223    func_mysqli_fetch_all_oo($link, $engine, "DATE", NULL, NULL, 430);
224
225    $datetime = @date('Y-m-d H:i:s');
226    func_mysqli_fetch_all_oo($link, $engine, "DATETIME", $datetime, $datetime, 440);
227    func_mysqli_fetch_all_oo($link, $engine, "DATETIME NOT NULL", $datetime, $datetime, 450);
228    func_mysqli_fetch_all_oo($link, $engine, "DATETIME", NULL, NULL, 460);
229
230    func_mysqli_fetch_all_oo($link, $engine, "TIMESTAMP", $datetime, $datetime, 470);
231
232    $time = @date('H:i:s');
233    func_mysqli_fetch_all_oo($link, $engine, "TIME", $time, $time, 480);
234    func_mysqli_fetch_all_oo($link, $engine, "TIME NOT NULL", $time, $time, 490);
235    func_mysqli_fetch_all_oo($link, $engine, "TIME", NULL, NULL, 500);
236
237    func_mysqli_fetch_all_oo($link, $engine, "YEAR", @date('Y'), @date('Y'), 510);
238    func_mysqli_fetch_all_oo($link, $engine, "YEAR NOT NULL", @date('Y'), @date('Y'), 520);
239    func_mysqli_fetch_all_oo($link, $engine, "YEAR", NULL, NULL, 530);
240
241    $string255 = func_mysqli_fetch_array_oo_make_string(255);
242    func_mysqli_fetch_all_oo($link, $engine, "CHAR(1)", "a", "a", 540);
243    func_mysqli_fetch_all_oo($link, $engine, "CHAR(255)", $string255,  $string255, 550);
244    func_mysqli_fetch_all_oo($link, $engine, "CHAR(1) NOT NULL", "a", "a", 560);
245    func_mysqli_fetch_all_oo($link, $engine, "CHAR(1)", NULL, NULL, 570);
246
247    $string65k = func_mysqli_fetch_array_oo_make_string(65400);
248    func_mysqli_fetch_all_oo($link, $engine, "VARCHAR(1)", "a", "a", 580);
249    func_mysqli_fetch_all_oo($link, $engine, "VARCHAR(255)", $string255, $string255, 590);
250    func_mysqli_fetch_all_oo($link, $engine, "VARCHAR(65400)", $string65k, $string65k, 600);
251    func_mysqli_fetch_all_oo($link, $engine, "VARCHAR(1) NOT NULL", "a", "a", 610);
252    func_mysqli_fetch_all_oo($link, $engine, "VARCHAR(1)", NULL, NULL, 620);
253
254    func_mysqli_fetch_all_oo($link, $engine, "BINARY(1)", "a", "a", 630);
255    func_mysqli_fetch_all_oo($link, $engine, "BINARY(2)", chr(0) . "a", chr(0) . "a", 640);
256    func_mysqli_fetch_all_oo($link, $engine, "BINARY(1) NOT NULL", "b", "b", 650);
257    func_mysqli_fetch_all_oo($link, $engine, "BINARY(1)", NULL, NULL, 660);
258
259    func_mysqli_fetch_all_oo($link, $engine, "VARBINARY(1)", "a", "a", 670);
260    func_mysqli_fetch_all_oo($link, $engine, "VARBINARY(2)", chr(0) . "a", chr(0) . "a", 680);
261    func_mysqli_fetch_all_oo($link, $engine, "VARBINARY(1) NOT NULL", "b", "b", 690);
262    func_mysqli_fetch_all_oo($link, $engine, "VARBINARY(1)", NULL, NULL, 700);
263
264    func_mysqli_fetch_all_oo($link, $engine, "TINYBLOB", "a", "a", 710);
265    func_mysqli_fetch_all_oo($link, $engine, "TINYBLOB", chr(0) . "a", chr(0) . "a", 720);
266    func_mysqli_fetch_all_oo($link, $engine, "TINYBLOB NOT NULL", "b", "b", 730);
267    func_mysqli_fetch_all_oo($link, $engine, "TINYBLOB", NULL, NULL, 740);
268
269    func_mysqli_fetch_all_oo($link, $engine, "TINYTEXT", "a", "a", 750);
270    func_mysqli_fetch_all_oo($link, $engine, "TINYTEXT NOT NULL", "a", "a", 760);
271    func_mysqli_fetch_all_oo($link, $engine, "TINYTEXT", NULL, NULL, 770);
272
273    func_mysqli_fetch_all_oo($link, $engine, "BLOB", "a", "a", 780);
274    func_mysqli_fetch_all_oo($link, $engine, "BLOB", chr(0) . "a", chr(0) . "a", 780);
275    func_mysqli_fetch_all_oo($link, $engine, "BLOB", NULL, NULL, 790);
276
277    func_mysqli_fetch_all_oo($link, $engine, "TEXT", "a", "a", 800);
278    func_mysqli_fetch_all_oo($link, $engine, "TEXT", chr(0) . "a", chr(0) . "a", 810);
279    func_mysqli_fetch_all_oo($link, $engine, "TEXT", NULL, NULL, 820);
280
281    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMBLOB", "a", "a", 830);
282    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMBLOB", chr(0) . "a", chr(0) . "a", 840);
283    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMBLOB", NULL, NULL, 850);
284
285    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMTEXT", "a", "a", 860);
286    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMTEXT", chr(0) . "a", chr(0) . "a", 870);
287    func_mysqli_fetch_all_oo($link, $engine, "MEDIUMTEXT", NULL, NULL, 880);
288
289    func_mysqli_fetch_all_oo($link, $engine, "LONGBLOB", "a", "a", 890);
290    func_mysqli_fetch_all_oo($link, $engine, "LONGTEXT", chr(0) . "a", chr(0) . "a", 900);
291    func_mysqli_fetch_all_oo($link, $engine, "LONGBLOB", NULL, NULL, 910);
292
293    func_mysqli_fetch_all_oo($link, $engine, "ENUM('a', 'b')", "a", "a", 920);
294    func_mysqli_fetch_all_oo($link, $engine, "ENUM('a', 'b')", NULL, NULL, 930);
295
296    func_mysqli_fetch_all_oo($link, $engine, "SET('a', 'b')", "a", "a", 940);
297    func_mysqli_fetch_all_oo($link, $engine, "SET('a', 'b')", NULL, NULL, 950);
298
299    mysqli_close($link);
300
301    if (false !== ($tmp = $res->fetch_array(MYSQLI_ASSOC)))
302        printf("[015] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
303
304    print "done!";
305?>
306--CLEAN--
307<?php
308    require_once("clean_table.inc");
309?>
310--EXPECTF--
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}
436
437Warning: mysqli_result::fetch_all(): Mode can be only MYSQLI_FETCH_NUM, MYSQLI_FETCH_ASSOC or MYSQLI_FETCH_BOTH in %s on line %d
438
439Warning: mysqli_result::fetch_array(): Couldn't fetch mysqli_result in %s on line %d
440done!
441