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(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 - %s\n", $offset + 1, $mysqli->errno, $mysqli->error, $sql);
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 - %s\n", $offset + 1, $mysqli->errno, $mysqli->error, $sql);
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
107        if ($regexp_comparison) {
108            if (!preg_match($regexp_comparison, (string)$row['label']) || !preg_match($regexp_comparison, (string)$row[1])) {
109                printf("[%04d] Expecting %s/%s [reg exp = %s], got %s/%s resp. %s/%s. [%d] %s\n", $offset + 4,
110                    gettype($php_value), $php_value, $regexp_comparison,
111                    gettype($row[1]), $row[1],
112                    gettype($row['label']), $row['label'], $mysqli->errno, $mysqli->error);
113                return false;
114            }
115        } else {
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 + 4,
118                    gettype($php_value), $php_value,
119                    gettype($row[1]), $row[1],
120                    gettype($row['label']), $row['label'], $mysqli->errno, $mysqli->error);
121                    return false;
122            }
123        }
124
125        return true;
126    }
127
128    function func_mysqli_fetch_array_make_string($len) {
129
130        $ret = '';
131        for ($i = 0; $i < $len; $i++)
132            $ret .= chr(mt_rand(65, 90));
133
134        return $ret;
135    }
136
137    func_mysqli_fetch_array($mysqli, $engine, "TINYINT", -11, "-11", 20);
138    func_mysqli_fetch_array($mysqli, $engine, "TINYINT", NULL, NULL, 30);
139    func_mysqli_fetch_array($mysqli, $engine, "TINYINT UNSIGNED", 1, "1", 40);
140    func_mysqli_fetch_array($mysqli, $engine, "TINYINT UNSIGNED", NULL, NULL, 50);
141
142    func_mysqli_fetch_array($mysqli, $engine, "BOOL", 1, "1", 60);
143    func_mysqli_fetch_array($mysqli, $engine, "BOOL", NULL, NULL, 70);
144    func_mysqli_fetch_array($mysqli, $engine, "BOOLEAN", 0, "0", 80);
145    func_mysqli_fetch_array($mysqli, $engine, "BOOLEAN", NULL, NULL, 90);
146
147    func_mysqli_fetch_array($mysqli, $engine, "SMALLINT", -32768, "-32768", 100);
148    func_mysqli_fetch_array($mysqli, $engine, "SMALLINT", 32767, "32767", 110);
149    func_mysqli_fetch_array($mysqli, $engine, "SMALLINT", NULL, NULL, 120);
150    func_mysqli_fetch_array($mysqli, $engine, "SMALLINT UNSIGNED", 65535, "65535", 130);
151    func_mysqli_fetch_array($mysqli, $engine, "SMALLINT UNSIGNED", NULL, NULL, 140);
152
153    func_mysqli_fetch_array($mysqli, $engine, "MEDIUMINT", -8388608, "-8388608", 150);
154    func_mysqli_fetch_array($mysqli, $engine, "MEDIUMINT", 8388607, "8388607", 160);
155    func_mysqli_fetch_array($mysqli, $engine, "MEDIUMINT", NULL, NULL, 170);
156    func_mysqli_fetch_array($mysqli, $engine, "MEDIUMINT UNSIGNED", 16777215, "16777215", 180);
157    func_mysqli_fetch_array($mysqli, $engine, "MEDIUMINT UNSIGNED", NULL, NULL, 190);
158
159    func_mysqli_fetch_array($mysqli, $engine, "INTEGER", -2147483648, "-2147483648", 200);
160    func_mysqli_fetch_array($mysqli, $engine, "INTEGER", 2147483647, "2147483647", 210);
161    func_mysqli_fetch_array($mysqli, $engine, "INTEGER", NULL, NULL, 220);
162    func_mysqli_fetch_array($mysqli, $engine, "INTEGER UNSIGNED", "4294967295", "4294967295", 230);
163    func_mysqli_fetch_array($mysqli, $engine, "INTEGER UNSIGNED", NULL, NULL, 240);
164
165    func_mysqli_fetch_array($mysqli, $engine, "BIGINT", "-9223372036854775808", "-9223372036854775808", 250);
166    func_mysqli_fetch_array($mysqli, $engine, "BIGINT", NULL, NULL, 260);
167    func_mysqli_fetch_array($mysqli, $engine, "BIGINT UNSIGNED", "18446744073709551615", "18446744073709551615", 270);
168    func_mysqli_fetch_array($mysqli, $engine, "BIGINT UNSIGNED", NULL, NULL, 280);
169
170    func_mysqli_fetch_array($mysqli, $engine, "FLOAT", (string)(-9223372036854775808 - 1.1), "-9.22337e+18", 290, "/-9\.22337e\+?[0]?18/iu");
171    func_mysqli_fetch_array($mysqli, $engine, "FLOAT", NULL, NULL, 300);
172    func_mysqli_fetch_array($mysqli, $engine, "FLOAT UNSIGNED", (string)(18446744073709551615 + 1.1), "1.84467e+?19", 310, "/1\.84467e\+?[0]?19/iu");
173    func_mysqli_fetch_array($mysqli, $engine, "FLOAT UNSIGNED ", NULL, NULL, 320);
174
175    func_mysqli_fetch_array($mysqli, $engine, "DOUBLE(10,2)", "-99999999.99", "-99999999.99", 330);
176    func_mysqli_fetch_array($mysqli, $engine, "DOUBLE(10,2)", NULL, NULL, 340);
177    func_mysqli_fetch_array($mysqli, $engine, "DOUBLE(10,2) UNSIGNED", "99999999.99", "99999999.99", 350);
178    func_mysqli_fetch_array($mysqli, $engine, "DOUBLE(10,2) UNSIGNED", NULL, NULL, 360);
179    func_mysqli_fetch_array($mysqli, $engine, "DECIMAL(10,2)", "-99999999.99", "-99999999.99", 370);
180    func_mysqli_fetch_array($mysqli, $engine, "DECIMAL(10,2)", NULL, NULL, 380);
181    func_mysqli_fetch_array($mysqli, $engine, "DECIMAL(10,2)", "99999999.99", "99999999.99", 390);
182    func_mysqli_fetch_array($mysqli, $engine, "DECIMAL(10,2)", NULL, NULL, 400);
183
184    // don't care about date() strict TZ warnings...
185    $date = @date('Y-m-d');
186    func_mysqli_fetch_array($mysqli, $engine, "DATE",$date, $date, 410);
187    func_mysqli_fetch_array($mysqli, $engine, "DATE NOT NULL",$date, $date, 420);
188    func_mysqli_fetch_array($mysqli, $engine, "DATE", NULL, NULL, 430);
189
190    $date = @date('Y-m-d H:i:s');
191    func_mysqli_fetch_array($mysqli, $engine, "DATETIME", $date, $date, 440);
192    func_mysqli_fetch_array($mysqli, $engine, "DATETIME NOT NULL", $date, $date, 450);
193    func_mysqli_fetch_array($mysqli, $engine, "DATETIME", NULL, NULL, 460);
194    func_mysqli_fetch_array($mysqli, $engine, "TIMESTAMP", $date, $date, 470);
195    $date = @date('H:i:s');
196    func_mysqli_fetch_array($mysqli, $engine, "TIME", $date, $date, 480);
197    func_mysqli_fetch_array($mysqli, $engine, "TIME NOT NULL", $date, $date, 490);
198    func_mysqli_fetch_array($mysqli, $engine, "TIME", NULL, NULL, 500);
199    func_mysqli_fetch_array($mysqli, $engine, "YEAR", @date('Y'), @date('Y'), 510);
200    func_mysqli_fetch_array($mysqli, $engine, "YEAR NOT NULL", @date('Y'), @date('Y'), 520);
201    func_mysqli_fetch_array($mysqli, $engine, "YEAR", NULL, NULL, 530);
202
203    $string255 = func_mysqli_fetch_array_make_string(255);
204    func_mysqli_fetch_array($mysqli, $engine, "CHAR(1)", "a", "a", 540);
205    func_mysqli_fetch_array($mysqli, $engine, "CHAR(255)", $string255,  $string255, 550);
206    func_mysqli_fetch_array($mysqli, $engine, "CHAR(1) NOT NULL", "a", "a", 560);
207    func_mysqli_fetch_array($mysqli, $engine, "CHAR(1)", NULL, NULL, 570);
208
209    $string65k = func_mysqli_fetch_array_make_string(65400);
210    func_mysqli_fetch_array($mysqli, $engine, "VARCHAR(1)", "a", "a", 580);
211    func_mysqli_fetch_array($mysqli, $engine, "VARCHAR(255)", $string255, $string255, 590);
212    func_mysqli_fetch_array($mysqli, $engine, "VARCHAR(65400)", $string65k, $string65k, 600);
213    func_mysqli_fetch_array($mysqli, $engine, "VARCHAR(1) NOT NULL", "a", "a", 610);
214    func_mysqli_fetch_array($mysqli, $engine, "VARCHAR(1)", NULL, NULL, 620);
215
216    func_mysqli_fetch_array($mysqli, $engine, "BINARY(1)", "a", "a", 630);
217    func_mysqli_fetch_array($mysqli, $engine, "BINARY(2)", chr(0) . "a", chr(0) . "a", 640);
218    func_mysqli_fetch_array($mysqli, $engine, "BINARY(1) NOT NULL", "b", "b", 650);
219    func_mysqli_fetch_array($mysqli, $engine, "BINARY(1)", NULL, NULL, 660);
220
221    func_mysqli_fetch_array($mysqli, $engine, "VARBINARY(1)", "a", "a", 670);
222    func_mysqli_fetch_array($mysqli, $engine, "VARBINARY(2)", chr(0) . "a", chr(0) . "a", 680);
223    func_mysqli_fetch_array($mysqli, $engine, "VARBINARY(1) NOT NULL", "b", "b", 690);
224    func_mysqli_fetch_array($mysqli, $engine, "VARBINARY(1)", NULL, NULL, 700);
225
226    func_mysqli_fetch_array($mysqli, $engine, "TINYBLOB", "a", "a", 710);
227    func_mysqli_fetch_array($mysqli, $engine, "TINYBLOB", chr(0) . "a", chr(0) . "a", 720);
228    func_mysqli_fetch_array($mysqli, $engine, "TINYBLOB NOT NULL", "b", "b", 730);
229    func_mysqli_fetch_array($mysqli, $engine, "TINYBLOB", NULL, NULL, 740);
230
231    func_mysqli_fetch_array($mysqli, $engine, "TINYTEXT", "a", "a", 750);
232    func_mysqli_fetch_array($mysqli, $engine, "TINYTEXT NOT NULL", "a", "a", 760);
233    func_mysqli_fetch_array($mysqli, $engine, "TINYTEXT", NULL, NULL, 770);
234
235    func_mysqli_fetch_array($mysqli, $engine, "BLOB", "a", "a", 780);
236    func_mysqli_fetch_array($mysqli, $engine, "BLOB", chr(0) . "a", chr(0) . "a", 780);
237    func_mysqli_fetch_array($mysqli, $engine, "BLOB", NULL, NULL, 790);
238
239    func_mysqli_fetch_array($mysqli, $engine, "TEXT", "a", "a", 800);
240    func_mysqli_fetch_array($mysqli, $engine, "TEXT", chr(0) . "a", chr(0) . "a", 810);
241    func_mysqli_fetch_array($mysqli, $engine, "TEXT", NULL, NULL, 820);
242
243    func_mysqli_fetch_array($mysqli, $engine, "MEDIUMBLOB", "a", "a", 830);
244    func_mysqli_fetch_array($mysqli, $engine, "MEDIUMBLOB", chr(0) . "a", chr(0) . "a", 840);
245    func_mysqli_fetch_array($mysqli, $engine, "MEDIUMBLOB", NULL, NULL, 850);
246
247    func_mysqli_fetch_array($mysqli, $engine, "MEDIUMTEXT", "a", "a", 860);
248    func_mysqli_fetch_array($mysqli, $engine, "MEDIUMTEXT", chr(0) . "a", chr(0) . "a", 870);
249    func_mysqli_fetch_array($mysqli, $engine, "MEDIUMTEXT", NULL, NULL, 880);
250
251    func_mysqli_fetch_array($mysqli, $engine, "LONGBLOB", "a", "a", 890);
252    func_mysqli_fetch_array($mysqli, $engine, "LONGTEXT", chr(0) . "a", chr(0) . "a", 900);
253    func_mysqli_fetch_array($mysqli, $engine, "LONGBLOB", NULL, NULL, 910);
254
255    func_mysqli_fetch_array($mysqli, $engine, "ENUM('a', 'b')", "a", "a", 920);
256    func_mysqli_fetch_array($mysqli, $engine, "ENUM('a', 'b')", NULL, NULL, 930);
257
258    func_mysqli_fetch_array($mysqli, $engine, "SET('a', 'b')", "a", "a", 940);
259    func_mysqli_fetch_array($mysqli, $engine, "SET('a', 'b')", NULL, NULL, 950);
260
261    $mysqli->close();
262
263    try {
264        $res->fetch_array(MYSQLI_ASSOC);
265    } catch (Error $exception) {
266        echo $exception->getMessage() . "\n";
267    }
268
269    print "done!";
270?>
271--CLEAN--
272<?php
273    require_once 'clean_table.inc';
274?>
275--EXPECT--
276[005]
277array(4) {
278  [0]=>
279  string(1) "1"
280  ["id"]=>
281  string(1) "1"
282  [1]=>
283  string(1) "a"
284  ["label"]=>
285  string(1) "a"
286}
287[006]
288array(2) {
289  [0]=>
290  string(1) "2"
291  [1]=>
292  string(1) "b"
293}
294[007]
295array(4) {
296  [0]=>
297  string(1) "3"
298  ["id"]=>
299  string(1) "3"
300  [1]=>
301  string(1) "c"
302  ["label"]=>
303  string(1) "c"
304}
305[008]
306array(2) {
307  ["id"]=>
308  string(1) "4"
309  ["label"]=>
310  string(1) "d"
311}
312[009]
313array(4) {
314  [0]=>
315  string(1) "5"
316  ["id"]=>
317  string(1) "5"
318  [1]=>
319  string(1) "e"
320  ["label"]=>
321  string(1) "e"
322}
323[011]
324array(11) {
325  [0]=>
326  string(1) "1"
327  ["a"]=>
328  string(1) "2"
329  [1]=>
330  string(1) "2"
331  [2]=>
332  string(1) "3"
333  ["c"]=>
334  string(1) "3"
335  [3]=>
336  string(1) "4"
337  ["C"]=>
338  string(1) "4"
339  [4]=>
340  NULL
341  ["d"]=>
342  NULL
343  [5]=>
344  string(1) "1"
345  ["e"]=>
346  string(1) "1"
347}
348mysqli_result::fetch_array(): Argument #1 ($mode) must be one of MYSQLI_NUM, MYSQLI_ASSOC, or MYSQLI_BOTH
349mysqli_result::fetch_array(): Argument #1 ($mode) must be one of MYSQLI_NUM, MYSQLI_ASSOC, or MYSQLI_BOTH
350mysqli_result object is already closed
351done!
352