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