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