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