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