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