1--TEST--
2mysqli_fetch_array() - all datatypes but BIT
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7?>
8--FILE--
9<?php
10    require_once("connect.inc");
11
12    require('table.inc');
13    if (!$res = mysqli_query($link, "SELECT * FROM test ORDER BY id LIMIT 5")) {
14        printf("[004] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
15    }
16
17    print "[005]\n";
18    var_dump(mysqli_fetch_array($res));
19
20    print "[006]\n";
21    var_dump(mysqli_fetch_array($res, MYSQLI_NUM));
22
23    print "[007]\n";
24    var_dump(mysqli_fetch_array($res, MYSQLI_BOTH));
25
26    print "[008]\n";
27    var_dump(mysqli_fetch_array($res, MYSQLI_ASSOC));
28
29    print "[009]\n";
30    var_dump(mysqli_fetch_array($res));
31
32    mysqli_free_result($res);
33
34    if (!$res = mysqli_query($link, "SELECT 1 AS a, 2 AS a, 3 AS c, 4 AS C, NULL AS d, true AS e")) {
35        printf("[010] Cannot run query, [%d] %s\n", mysqli_errno($link), $mysqli_error($link));
36    }
37    print "[011]\n";
38    var_dump(mysqli_fetch_array($res, MYSQLI_BOTH));
39
40    mysqli_free_result($res);
41    if (!$res = mysqli_query($link, "SELECT 1 AS a, 2 AS b, 3 AS c, 4 AS C")) {
42        printf("[012] Cannot run query, [%d] %s\n",
43            mysqli_errno($link), $mysqli_error($link));
44        exit(1);
45    }
46
47    do {
48        $illegal_mode = mt_rand(-10000, 10000);
49    } while (in_array($illegal_mode, array(MYSQLI_ASSOC, MYSQLI_NUM, MYSQLI_BOTH)));
50    // NOTE: for BC reasons with ext/mysql, ext/mysqli accepts invalid result modes.
51    try {
52        mysqli_fetch_array($res, $illegal_mode);
53    } catch (ValueError $exception) {
54        echo $exception->getMessage() . "\n";
55    }
56
57    try {
58        mysqli_fetch_array($res, $illegal_mode);
59    } catch (ValueError $exception) {
60        echo $exception->getMessage() . "\n";
61    }
62
63    mysqli_free_result($res);
64
65    function func_mysqli_fetch_array($link, $engine, $sql_type, $sql_value, $php_value, $offset, $regexp_comparison = NULL, $binary_type = false) {
66
67        if (!mysqli_query($link, "DROP TABLE IF EXISTS test")) {
68            printf("[%04d] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
69            return false;
70        }
71
72        if (!mysqli_query($link, $sql = sprintf("CREATE TABLE test(id INT NOT NULL, label %s, PRIMARY KEY(id)) ENGINE = %s", $sql_type, $engine))) {
73                // don't bail, engine might not support the datatype
74                return false;
75        }
76
77        if (is_null($php_value)) {
78            if (!mysqli_query($link, $sql = sprintf("INSERT INTO test(id, label) VALUES (1, NULL)"))) {
79                printf("[%04d] [%d] %s\n", $offset + 1, mysqli_errno($link), mysqli_error($link));
80                return false;
81            }
82        } else {
83            if (is_string($sql_value)) {
84                if (!mysqli_query($link, $sql = "INSERT INTO test(id, label) VALUES (1, '" . $sql_value . "')")) {
85                    printf("[%04ds] [%d] %s - %s\n", $offset + 1, mysqli_errno($link), mysqli_error($link), $sql);
86                    return false;
87                }
88            } else {
89                if (!mysqli_query($link, $sql = sprintf("INSERT INTO test(id, label) VALUES (1, '%d')", $sql_value))) {
90                    printf("[%04di] [%d] %s\n", $offset + 1, mysqli_errno($link), mysqli_error($link));
91                    return false;
92                }
93            }
94        }
95        if (!$res = mysqli_query($link, "SELECT id, label FROM test")) {
96            printf("[%04d] [%d] %s\n", $offset + 2, mysqli_errno($link), mysqli_error($link));
97            return false;
98        }
99
100        if (!$row = mysqli_fetch_array($res, MYSQLI_BOTH)) {
101            printf("[%04d] [%d] %s\n", $offset + 3, mysqli_errno($link), mysqli_error($link));
102            return false;
103        }
104
105        if ($regexp_comparison) {
106            if (!preg_match($regexp_comparison, (string)$row['label']) || !preg_match($regexp_comparison, (string)$row[1])) {
107                printf("[%04d] Expecting %s/%s [reg exp = %s], got %s/%s resp. %s/%s. [%d] %s\n", $offset + 4,
108                    gettype($php_value), $php_value, $regexp_comparison,
109                    gettype($row[1]), $row[1],
110                    gettype($row['label']), $row['label'], mysqli_errno($link), mysqli_error($link));
111                return false;
112            }
113        } else if ((gettype($php_value) == 'unicode') && $binary_type) {
114            // Unicode is on and we are told that the MySQL column type is a binary type.
115            // Don't expect a unicode value from the database, you'll get binary string
116            if (($row['label'] != $php_value) || ($row[1] != $php_value)) {
117                printf("[%04d] Expecting %s/%s, got %s/%s resp. %s/%s. [%d] %s\n", $offset + 5,
118                    gettype($php_value), $php_value,
119                    gettype($row[1]), $row[1],
120                    gettype($row['label']), $row['label'], mysqli_errno($link), mysqli_error($link));
121                return false;
122            }
123            if (gettype($row['label']) == 'unicode') {
124                var_dump(mysqli_fetch_field_direct($res, 1), $row['label']);
125                printf("[%04d] SQL Type: '%s', binary columns are supposed to return binary string and not unicode\n",
126                    $offset + 6, $sql_type);
127                return false;
128            }
129        } else {
130            if (($row['label'] !== $php_value) || ($row[1] != $php_value)) {
131                printf("[%04d] Expecting %s/%s, got %s/%s resp. %s/%s. [%d] %s\n", $offset + 7,
132                    gettype($php_value), $php_value,
133                    gettype($row[1]), $row[1],
134                    gettype($row['label']), $row['label'], mysqli_errno($link), mysqli_error($link));
135                return false;
136            }
137        }
138        return true;
139    }
140
141    function func_mysqli_fetch_array_make_string($len) {
142
143        $ret = '';
144        for ($i = 0; $i < $len; $i++)
145                $ret .= chr(mt_rand(65, 90));
146
147        return $ret;
148    }
149
150    func_mysqli_fetch_array($link, $engine, "TINYINT", -11, "-11", 20);
151    func_mysqli_fetch_array($link, $engine, "TINYINT", NULL, NULL, 30);
152    func_mysqli_fetch_array($link, $engine, "TINYINT UNSIGNED", 1, "1", 40);
153    func_mysqli_fetch_array($link, $engine, "TINYINT UNSIGNED", NULL, NULL, 50);
154
155    func_mysqli_fetch_array($link, $engine, "BOOL", 1, "1", 60);
156    func_mysqli_fetch_array($link, $engine, "BOOL", NULL, NULL, 70);
157    func_mysqli_fetch_array($link, $engine, "BOOLEAN", 0, "0", 80);
158    func_mysqli_fetch_array($link, $engine, "BOOLEAN", NULL, NULL, 90);
159
160    func_mysqli_fetch_array($link, $engine, "SMALLINT", -32768, "-32768", 100);
161    func_mysqli_fetch_array($link, $engine, "SMALLINT", 32767, "32767", 110);
162    func_mysqli_fetch_array($link, $engine, "SMALLINT", NULL, NULL, 120);
163    func_mysqli_fetch_array($link, $engine, "SMALLINT UNSIGNED", 65535, "65535", 130);
164    func_mysqli_fetch_array($link, $engine, "SMALLINT UNSIGNED", NULL, NULL, 140);
165
166    func_mysqli_fetch_array($link, $engine, "MEDIUMINT", -8388608, "-8388608", 150);
167    func_mysqli_fetch_array($link, $engine, "MEDIUMINT", 8388607, "8388607", 160);
168    func_mysqli_fetch_array($link, $engine, "MEDIUMINT", NULL, NULL, 170);
169    func_mysqli_fetch_array($link, $engine, "MEDIUMINT UNSIGNED", 16777215, "16777215", 180);
170    func_mysqli_fetch_array($link, $engine, "MEDIUMINT UNSIGNED", NULL, NULL, 190);
171
172    func_mysqli_fetch_array($link, $engine, "INTEGER", -2147483648, "-2147483648", 200);
173    func_mysqli_fetch_array($link, $engine, "INTEGER", 2147483647, "2147483647", 210);
174    func_mysqli_fetch_array($link, $engine, "INTEGER", NULL, NULL, 220);
175    func_mysqli_fetch_array($link, $engine, "INTEGER UNSIGNED", "4294967295", "4294967295", 230);
176    func_mysqli_fetch_array($link, $engine, "INTEGER UNSIGNED", NULL, NULL, 240);
177
178    if ($IS_MYSQLND || mysqli_get_server_version($link) >= 51000) {
179        func_mysqli_fetch_array($link, $engine, "BIGINT", "-9223372036854775808", "-9223372036854775808", 250);
180        func_mysqli_fetch_array($link, $engine, "BIGINT", NULL, NULL, 260);
181        func_mysqli_fetch_array($link, $engine, "BIGINT UNSIGNED", "18446744073709551615", "18446744073709551615", 260);
182        func_mysqli_fetch_array($link, $engine, "BIGINT UNSIGNED", NULL, NULL, 280);
183    }
184
185    func_mysqli_fetch_array($link, $engine, "FLOAT", (string)(-9223372036854775808 - 1.1), "-9.22337e+18", 290, "/-9\.22337e\+?[0]?18/iu");
186    func_mysqli_fetch_array($link, $engine, "FLOAT", NULL, NULL, 300);
187    func_mysqli_fetch_array($link, $engine, "FLOAT UNSIGNED", (string)(18446744073709551615 + 1.1), "1.84467e+?19", 310, "/1\.84467e\+?[0]?19/iu");
188    func_mysqli_fetch_array($link, $engine, "FLOAT UNSIGNED ", NULL, NULL, 320);
189
190    func_mysqli_fetch_array($link, $engine, "DOUBLE(10,2)", "-99999999.99", "-99999999.99", 330);
191    func_mysqli_fetch_array($link, $engine, "DOUBLE(10,2)", NULL, NULL, 340);
192    func_mysqli_fetch_array($link, $engine, "DOUBLE(10,2) UNSIGNED", "99999999.99", "99999999.99", 350);
193    func_mysqli_fetch_array($link, $engine, "DOUBLE(10,2) UNSIGNED", NULL, NULL, 360);
194
195    func_mysqli_fetch_array($link, $engine, "DECIMAL(10,2)", "-99999999.99", "-99999999.99", 370);
196    func_mysqli_fetch_array($link, $engine, "DECIMAL(10,2)", NULL, NULL, 380);
197    func_mysqli_fetch_array($link, $engine, "DECIMAL(10,2)", "99999999.99", "99999999.99", 390);
198    func_mysqli_fetch_array($link, $engine, "DECIMAL(10,2)", NULL, NULL, 400);
199
200        // don't care about date() strict TZ warnings...
201    $date = @date('Y-m-d');
202    func_mysqli_fetch_array($link, $engine, "DATE",$date, $date, 410);
203    func_mysqli_fetch_array($link, $engine, "DATE NOT NULL",$date, $date, 420);
204    func_mysqli_fetch_array($link, $engine, "DATE", NULL, NULL, 430);
205
206    $date = @date('Y-m-d H:i:s');
207    func_mysqli_fetch_array($link, $engine, "DATETIME", $date, $date, 440);
208    func_mysqli_fetch_array($link, $engine, "DATETIME NOT NULL", $date, $date, 450);
209    func_mysqli_fetch_array($link, $engine, "DATETIME", NULL, NULL, 460);
210    func_mysqli_fetch_array($link, $engine, "TIMESTAMP", $date, $date, 470);
211
212    $date = @date('H:i:s');
213    func_mysqli_fetch_array($link, $engine, "TIME", $date, $date, 480);
214    func_mysqli_fetch_array($link, $engine, "TIME NOT NULL", $date, $date, 490);
215    func_mysqli_fetch_array($link, $engine, "TIME", NULL, NULL, 500);
216
217    func_mysqli_fetch_array($link, $engine, "YEAR", @date('Y'), @date('Y'), 510);
218    func_mysqli_fetch_array($link, $engine, "YEAR NOT NULL", @date('Y'), @date('Y'), 520);
219    func_mysqli_fetch_array($link, $engine, "YEAR", NULL, NULL, 530);
220
221    $string255 = func_mysqli_fetch_array_make_string(255);
222    func_mysqli_fetch_array($link, $engine, "CHAR(1)", "a", "a", 540);
223    func_mysqli_fetch_array($link, $engine, "CHAR(255)", $string255,  $string255, 550);
224    func_mysqli_fetch_array($link, $engine, "CHAR(1) NOT NULL", "a", "a", 560);
225    func_mysqli_fetch_array($link, $engine, "CHAR(1)", NULL, NULL, 570);
226
227    $string65k = func_mysqli_fetch_array_make_string(65400);
228    func_mysqli_fetch_array($link, $engine, "VARCHAR(1)", "a", "a", 580);
229    func_mysqli_fetch_array($link, $engine, "VARCHAR(255)", $string255, $string255, 590);
230    func_mysqli_fetch_array($link, $engine, "VARCHAR(65400)", $string65k, $string65k, 600);
231    func_mysqli_fetch_array($link, $engine, "VARCHAR(1) NOT NULL", "a", "a", 610);
232    func_mysqli_fetch_array($link, $engine, "VARCHAR(1)", NULL, NULL, 620);
233
234    func_mysqli_fetch_array($link, $engine, "BINARY(1)", "a", "a", 630, null, true);
235    func_mysqli_fetch_array($link, $engine, "BINARY(2)", chr(0) . "a", chr(0) . "a", 640, null, true);
236    func_mysqli_fetch_array($link, $engine, "BINARY(1) NOT NULL", "b", "b", 650, null, true);
237    func_mysqli_fetch_array($link, $engine, "BINARY(1)", NULL, NULL, 660, null, true);
238
239    func_mysqli_fetch_array($link, $engine, "VARBINARY(1)", "a", "a", 670, null, true);
240    func_mysqli_fetch_array($link, $engine, "VARBINARY(2)", chr(0) . "a", chr(0) . "a", 680, null, true);
241    func_mysqli_fetch_array($link, $engine, "VARBINARY(1) NOT NULL", "b", "b", 690, null, true);
242    func_mysqli_fetch_array($link, $engine, "VARBINARY(1)", NULL, NULL, 700, null, true);
243
244    func_mysqli_fetch_array($link, $engine, "TINYBLOB", "a", "a", 710, null, true);
245    func_mysqli_fetch_array($link, $engine, "TINYBLOB", chr(0) . "a", chr(0) . "a", 720, null, true);
246    func_mysqli_fetch_array($link, $engine, "TINYBLOB NOT NULL", "b", "b", 730, null, true);
247    func_mysqli_fetch_array($link, $engine, "TINYBLOB", NULL, NULL, 740, null, true);
248
249    func_mysqli_fetch_array($link, $engine, "TINYTEXT", "a", "a", 750);
250    func_mysqli_fetch_array($link, $engine, "TINYTEXT NOT NULL", "a", "a", 760);
251    func_mysqli_fetch_array($link, $engine, "TINYTEXT", NULL, NULL, 770);
252
253    func_mysqli_fetch_array($link, $engine, "BLOB", "a", "a", 780, null, true);
254    func_mysqli_fetch_array($link, $engine, "BLOB", chr(0) . "a", chr(0) . "a", 780, null, true);
255    func_mysqli_fetch_array($link, $engine, "BLOB", NULL, NULL, 790, null, true);
256
257    func_mysqli_fetch_array($link, $engine, "TEXT", "a", "a", 800);
258    func_mysqli_fetch_array($link, $engine, "TEXT", chr(0) . "a", chr(0) . "a", 810);
259    func_mysqli_fetch_array($link, $engine, "TEXT", NULL, NULL, 820);
260
261    func_mysqli_fetch_array($link, $engine, "MEDIUMBLOB", "a", "a", 830, null, true);
262    func_mysqli_fetch_array($link, $engine, "MEDIUMBLOB", chr(0) . "a", chr(0) . "a", 840, null, true);
263    func_mysqli_fetch_array($link, $engine, "MEDIUMBLOB", NULL, NULL, 850, null, true);
264
265    func_mysqli_fetch_array($link, $engine, "MEDIUMTEXT", "a", "a", 860);
266    func_mysqli_fetch_array($link, $engine, "MEDIUMTEXT", chr(0) . "a", chr(0) . "a", 870);
267    func_mysqli_fetch_array($link, $engine, "MEDIUMTEXT", NULL, NULL, 880);
268
269    func_mysqli_fetch_array($link, $engine, "LONGBLOB", "a", "a", 890, null, true);
270    func_mysqli_fetch_array($link, $engine, "LONGTEXT", chr(0) . "a", chr(0) . "a", 900);
271    func_mysqli_fetch_array($link, $engine, "LONGBLOB", NULL, NULL, 910, null, true);
272
273    func_mysqli_fetch_array($link, $engine, "ENUM('a', 'b')", "a", "a", 920);
274    func_mysqli_fetch_array($link, $engine, "ENUM('a', 'b')", NULL, NULL, 930);
275
276    func_mysqli_fetch_array($link, $engine, "SET('a', 'b')", "a", "a", 940);
277    func_mysqli_fetch_array($link, $engine, "SET('a', 'b')", NULL, NULL, 950);
278
279    mysqli_close($link);
280
281    try {
282        mysqli_fetch_array($res, MYSQLI_ASSOC);
283    } catch (Error $exception) {
284        echo $exception->getMessage() . "\n";
285    }
286
287    print "done!";
288?>
289--EXPECTF--
290[005]
291array(4) {
292  [0]=>
293  string(1) "1"
294  ["id"]=>
295  string(1) "1"
296  [1]=>
297  string(1) "a"
298  ["label"]=>
299  string(1) "a"
300}
301[006]
302array(2) {
303  [0]=>
304  string(1) "2"
305  [1]=>
306  string(1) "b"
307}
308[007]
309array(4) {
310  [0]=>
311  string(1) "3"
312  ["id"]=>
313  string(1) "3"
314  [1]=>
315  string(1) "c"
316  ["label"]=>
317  string(1) "c"
318}
319[008]
320array(2) {
321  ["id"]=>
322  string(1) "4"
323  ["label"]=>
324  string(1) "d"
325}
326[009]
327array(4) {
328  [0]=>
329  string(1) "5"
330  ["id"]=>
331  string(1) "5"
332  [1]=>
333  string(1) "e"
334  ["label"]=>
335  string(1) "e"
336}
337[011]
338array(11) {
339  [0]=>
340  string(1) "1"
341  ["a"]=>
342  string(1) "2"
343  [1]=>
344  string(1) "2"
345  [2]=>
346  string(1) "3"
347  ["c"]=>
348  string(1) "3"
349  [3]=>
350  string(1) "4"
351  ["C"]=>
352  string(1) "4"
353  [4]=>
354  NULL
355  ["d"]=>
356  NULL
357  [5]=>
358  string(1) "1"
359  ["e"]=>
360  string(1) "1"
361}
362mysqli_fetch_array(): Argument #2 ($mode) must be one of MYSQLI_NUM, MYSQLI_ASSOC, or MYSQLI_BOTH
363mysqli_fetch_array(): Argument #2 ($mode) must be one of MYSQLI_NUM, MYSQLI_ASSOC, or MYSQLI_BOTH
364mysqli_result object is already closed
365done!
366