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