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