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    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    func_mysqli_fetch_array($mysqli, $engine, "FLOAT", (string)(-9223372036854775808 - 1.1), "-9.22337e+18", 290, "/-9\.22337e\+?[0]?18/iu");
180    func_mysqli_fetch_array($mysqli, $engine, "FLOAT", NULL, NULL, 300);
181    func_mysqli_fetch_array($mysqli, $engine, "FLOAT UNSIGNED", (string)(18446744073709551615 + 1.1), "1.84467e+?19", 310, "/1\.84467e\+?[0]?19/iu");
182    func_mysqli_fetch_array($mysqli, $engine, "FLOAT UNSIGNED ", NULL, NULL, 320);
183
184    func_mysqli_fetch_array($mysqli, $engine, "DOUBLE(10,2)", "-99999999.99", "-99999999.99", 330);
185    func_mysqli_fetch_array($mysqli, $engine, "DOUBLE(10,2)", NULL, NULL, 340);
186    func_mysqli_fetch_array($mysqli, $engine, "DOUBLE(10,2) UNSIGNED", "99999999.99", "99999999.99", 350);
187    func_mysqli_fetch_array($mysqli, $engine, "DOUBLE(10,2) UNSIGNED", NULL, NULL, 360);
188    func_mysqli_fetch_array($mysqli, $engine, "DECIMAL(10,2)", "-99999999.99", "-99999999.99", 370);
189    func_mysqli_fetch_array($mysqli, $engine, "DECIMAL(10,2)", NULL, NULL, 380);
190    func_mysqli_fetch_array($mysqli, $engine, "DECIMAL(10,2)", "99999999.99", "99999999.99", 390);
191    func_mysqli_fetch_array($mysqli, $engine, "DECIMAL(10,2)", NULL, NULL, 400);
192
193    // don't care about date() strict TZ warnings...
194    $date = @date('Y-m-d');
195    func_mysqli_fetch_array($mysqli, $engine, "DATE",$date, $date, 410);
196    func_mysqli_fetch_array($mysqli, $engine, "DATE NOT NULL",$date, $date, 420);
197    func_mysqli_fetch_array($mysqli, $engine, "DATE", NULL, NULL, 430);
198
199    $date = @date('Y-m-d H:i:s');
200    func_mysqli_fetch_array($mysqli, $engine, "DATETIME", $date, $date, 440);
201    func_mysqli_fetch_array($mysqli, $engine, "DATETIME NOT NULL", $date, $date, 450);
202    func_mysqli_fetch_array($mysqli, $engine, "DATETIME", NULL, NULL, 460);
203    func_mysqli_fetch_array($mysqli, $engine, "TIMESTAMP", $date, $date, 470);
204    $date = @date('H:i:s');
205    func_mysqli_fetch_array($mysqli, $engine, "TIME", $date, $date, 480);
206    func_mysqli_fetch_array($mysqli, $engine, "TIME NOT NULL", $date, $date, 490);
207    func_mysqli_fetch_array($mysqli, $engine, "TIME", NULL, NULL, 500);
208    func_mysqli_fetch_array($mysqli, $engine, "YEAR", @date('Y'), @date('Y'), 510);
209    func_mysqli_fetch_array($mysqli, $engine, "YEAR NOT NULL", @date('Y'), @date('Y'), 520);
210    func_mysqli_fetch_array($mysqli, $engine, "YEAR", NULL, NULL, 530);
211
212    $string255 = func_mysqli_fetch_array_make_string(255);
213    func_mysqli_fetch_array($mysqli, $engine, "CHAR(1)", "a", "a", 540);
214    func_mysqli_fetch_array($mysqli, $engine, "CHAR(255)", $string255,  $string255, 550);
215    func_mysqli_fetch_array($mysqli, $engine, "CHAR(1) NOT NULL", "a", "a", 560);
216    func_mysqli_fetch_array($mysqli, $engine, "CHAR(1)", NULL, NULL, 570);
217
218    $string65k = func_mysqli_fetch_array_make_string(65400);
219    func_mysqli_fetch_array($mysqli, $engine, "VARCHAR(1)", "a", "a", 580);
220    func_mysqli_fetch_array($mysqli, $engine, "VARCHAR(255)", $string255, $string255, 590);
221    func_mysqli_fetch_array($mysqli, $engine, "VARCHAR(65400)", $string65k, $string65k, 600);
222    func_mysqli_fetch_array($mysqli, $engine, "VARCHAR(1) NOT NULL", "a", "a", 610);
223    func_mysqli_fetch_array($mysqli, $engine, "VARCHAR(1)", NULL, NULL, 620);
224
225    func_mysqli_fetch_array($mysqli, $engine, "BINARY(1)", "a", "a", 630);
226    func_mysqli_fetch_array($mysqli, $engine, "BINARY(2)", chr(0) . "a", chr(0) . "a", 640);
227    func_mysqli_fetch_array($mysqli, $engine, "BINARY(1) NOT NULL", "b", "b", 650);
228    func_mysqli_fetch_array($mysqli, $engine, "BINARY(1)", NULL, NULL, 660);
229
230    func_mysqli_fetch_array($mysqli, $engine, "VARBINARY(1)", "a", "a", 670);
231    func_mysqli_fetch_array($mysqli, $engine, "VARBINARY(2)", chr(0) . "a", chr(0) . "a", 680);
232    func_mysqli_fetch_array($mysqli, $engine, "VARBINARY(1) NOT NULL", "b", "b", 690);
233    func_mysqli_fetch_array($mysqli, $engine, "VARBINARY(1)", NULL, NULL, 700);
234
235    func_mysqli_fetch_array($mysqli, $engine, "TINYBLOB", "a", "a", 710);
236    func_mysqli_fetch_array($mysqli, $engine, "TINYBLOB", chr(0) . "a", chr(0) . "a", 720);
237    func_mysqli_fetch_array($mysqli, $engine, "TINYBLOB NOT NULL", "b", "b", 730);
238    func_mysqli_fetch_array($mysqli, $engine, "TINYBLOB", NULL, NULL, 740);
239
240    func_mysqli_fetch_array($mysqli, $engine, "TINYTEXT", "a", "a", 750);
241    func_mysqli_fetch_array($mysqli, $engine, "TINYTEXT NOT NULL", "a", "a", 760);
242    func_mysqli_fetch_array($mysqli, $engine, "TINYTEXT", NULL, NULL, 770);
243
244    func_mysqli_fetch_array($mysqli, $engine, "BLOB", "a", "a", 780);
245    func_mysqli_fetch_array($mysqli, $engine, "BLOB", chr(0) . "a", chr(0) . "a", 780);
246    func_mysqli_fetch_array($mysqli, $engine, "BLOB", NULL, NULL, 790);
247
248    func_mysqli_fetch_array($mysqli, $engine, "TEXT", "a", "a", 800);
249    func_mysqli_fetch_array($mysqli, $engine, "TEXT", chr(0) . "a", chr(0) . "a", 810);
250    func_mysqli_fetch_array($mysqli, $engine, "TEXT", NULL, NULL, 820);
251
252    func_mysqli_fetch_array($mysqli, $engine, "MEDIUMBLOB", "a", "a", 830);
253    func_mysqli_fetch_array($mysqli, $engine, "MEDIUMBLOB", chr(0) . "a", chr(0) . "a", 840);
254    func_mysqli_fetch_array($mysqli, $engine, "MEDIUMBLOB", NULL, NULL, 850);
255
256    func_mysqli_fetch_array($mysqli, $engine, "MEDIUMTEXT", "a", "a", 860);
257    func_mysqli_fetch_array($mysqli, $engine, "MEDIUMTEXT", chr(0) . "a", chr(0) . "a", 870);
258    func_mysqli_fetch_array($mysqli, $engine, "MEDIUMTEXT", NULL, NULL, 880);
259
260    func_mysqli_fetch_array($mysqli, $engine, "LONGBLOB", "a", "a", 890);
261    func_mysqli_fetch_array($mysqli, $engine, "LONGTEXT", chr(0) . "a", chr(0) . "a", 900);
262    func_mysqli_fetch_array($mysqli, $engine, "LONGBLOB", NULL, NULL, 910);
263
264    func_mysqli_fetch_array($mysqli, $engine, "ENUM('a', 'b')", "a", "a", 920);
265    func_mysqli_fetch_array($mysqli, $engine, "ENUM('a', 'b')", NULL, NULL, 930);
266
267    func_mysqli_fetch_array($mysqli, $engine, "SET('a', 'b')", "a", "a", 940);
268    func_mysqli_fetch_array($mysqli, $engine, "SET('a', 'b')", NULL, NULL, 950);
269
270    $mysqli->close();
271
272    try {
273        $res->fetch_array(MYSQLI_ASSOC);
274    } catch (Error $exception) {
275        echo $exception->getMessage() . "\n";
276    }
277
278    print "done!";
279?>
280--CLEAN--
281<?php
282    require_once("clean_table.inc");
283?>
284--EXPECT--
285[005]
286array(4) {
287  [0]=>
288  string(1) "1"
289  ["id"]=>
290  string(1) "1"
291  [1]=>
292  string(1) "a"
293  ["label"]=>
294  string(1) "a"
295}
296[006]
297array(2) {
298  [0]=>
299  string(1) "2"
300  [1]=>
301  string(1) "b"
302}
303[007]
304array(4) {
305  [0]=>
306  string(1) "3"
307  ["id"]=>
308  string(1) "3"
309  [1]=>
310  string(1) "c"
311  ["label"]=>
312  string(1) "c"
313}
314[008]
315array(2) {
316  ["id"]=>
317  string(1) "4"
318  ["label"]=>
319  string(1) "d"
320}
321[009]
322array(4) {
323  [0]=>
324  string(1) "5"
325  ["id"]=>
326  string(1) "5"
327  [1]=>
328  string(1) "e"
329  ["label"]=>
330  string(1) "e"
331}
332[011]
333array(11) {
334  [0]=>
335  string(1) "1"
336  ["a"]=>
337  string(1) "2"
338  [1]=>
339  string(1) "2"
340  [2]=>
341  string(1) "3"
342  ["c"]=>
343  string(1) "3"
344  [3]=>
345  string(1) "4"
346  ["C"]=>
347  string(1) "4"
348  [4]=>
349  NULL
350  ["d"]=>
351  NULL
352  [5]=>
353  string(1) "1"
354  ["e"]=>
355  string(1) "1"
356}
357mysqli_result::fetch_array(): Argument #1 ($mode) must be one of MYSQLI_NUM, MYSQLI_ASSOC, or MYSQLI_BOTH
358mysqli_result::fetch_array(): Argument #1 ($mode) must be one of MYSQLI_NUM, MYSQLI_ASSOC, or MYSQLI_BOTH
359mysqli_result object is already closed
360done!
361