1--TEST--
2mysqli->fetch_array()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
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	$tmp = $res->fetch_array($illegal_mode);
60	if (false !== $tmp)
61		printf("[013] Expecting boolean/false although, got %s/%s. [%d] %s\n",
62			gettype($tmp), $tmp, $mysqli->errno, $mysqli->error);
63
64	$tmp = $res->fetch_array($illegal_mode);
65	if (false !== $tmp)
66		printf("[014] Expecting boolean/false, got %s/%s. [%d] %s\n",
67			gettype($tmp), $tmp, $mysqli->errno, $mysqli->error);
68
69	$res->free_result();
70
71	function func_mysqli_fetch_array($mysqli, $engine, $sql_type, $sql_value, $php_value, $offset, $regexp_comparison = NULL) {
72
73		if (!$mysqli->query("DROP TABLE IF EXISTS test")) {
74			printf("[%04d] [%d] %s\n", $offset, $mysqli->errno, $mysqli->error);
75			return false;
76		}
77
78		if (!$mysqli->query($sql = sprintf("CREATE TABLE test(id INT NOT NULL, label %s, PRIMARY KEY(id)) ENGINE = %s", $sql_type, $engine))) {
79				// don't bail, engine might not support the datatype
80				return false;
81		}
82
83		if (is_null($php_value)) {
84			if (!$mysqli->query($sql = sprintf("INSERT INTO test(id, label) VALUES (1, NULL)"))) {
85				printf("[%04d] [%d] %s\n", $offset + 1, $mysqli->errno, $mysqli->error);
86				return false;
87			}
88		} else {
89			if (is_string($sql_value)) {
90				if (!$mysqli->query($sql = "INSERT INTO test(id, label) VALUES (1, '" . $sql_value . "')")) {
91					printf("[%04ds] [%d] %s - %s\n", $offset + 1, $mysqli->errno, $mysqli->error, $sql);
92					return false;
93				}
94			} else {
95				if (!$mysqli->query($sql = sprintf("INSERT INTO test(id, label) VALUES (1, '%d')", $sql_value))) {
96					printf("[%04di] [%d] %s\n", $offset + 1, $mysqli->errno, $mysqli->error);
97					return false;
98				}
99			}
100		}
101
102		if (!$res = $mysqli->query("SELECT id, label FROM test")) {
103			printf("[%04d] [%d] %s\n", $offset + 2, $mysqli->errno, $mysqli->error);
104			return false;
105		}
106
107		if (!$row = $res->fetch_array(MYSQLI_BOTH)) {
108			printf("[%04d] [%d] %s\n", $offset + 3, $mysqli->errno, $mysqli->error);
109			return false;
110		}
111		$fields = mysqli_fetch_fields($res);
112
113		if (!(gettype($php_value)=="unicode" && ($fields[1]->flags & 128))) {
114			if ($regexp_comparison) {
115				if (!preg_match($regexp_comparison, (string)$row['label']) || !preg_match($regexp_comparison, (string)$row[1])) {
116					printf("[%04d] Expecting %s/%s [reg exp = %s], got %s/%s resp. %s/%s. [%d] %s\n", $offset + 4,
117						gettype($php_value), $php_value, $regexp_comparison,
118						gettype($row[1]), $row[1],
119						gettype($row['label']), $row['label'], $mysqli->errno, $mysqli->error);
120					return false;
121				}
122			} else {
123				if (($row['label'] !== $php_value) || ($row[1] != $php_value)) {
124					printf("[%04d] Expecting %s/%s, got %s/%s resp. %s/%s. [%d] %s\n", $offset + 4,
125						gettype($php_value), $php_value,
126						gettype($row[1]), $row[1],
127						gettype($row['label']), $row['label'], $mysqli->errno, $mysqli->error);
128						return false;
129				}
130			}
131		}
132		return true;
133	}
134
135	function func_mysqli_fetch_array_make_string($len) {
136
137		$ret = '';
138		for ($i = 0; $i < $len; $i++)
139			$ret .= chr(mt_rand(65, 90));
140
141		return $ret;
142	}
143
144	func_mysqli_fetch_array($mysqli, $engine, "TINYINT", -11, "-11", 20);
145	func_mysqli_fetch_array($mysqli, $engine, "TINYINT", NULL, NULL, 30);
146	func_mysqli_fetch_array($mysqli, $engine, "TINYINT UNSIGNED", 1, "1", 40);
147	func_mysqli_fetch_array($mysqli, $engine, "TINYINT UNSIGNED", NULL, NULL, 50);
148
149	func_mysqli_fetch_array($mysqli, $engine, "BOOL", 1, "1", 60);
150	func_mysqli_fetch_array($mysqli, $engine, "BOOL", NULL, NULL, 70);
151	func_mysqli_fetch_array($mysqli, $engine, "BOOLEAN", 0, "0", 80);
152	func_mysqli_fetch_array($mysqli, $engine, "BOOLEAN", NULL, NULL, 90);
153
154	func_mysqli_fetch_array($mysqli, $engine, "SMALLINT", -32768, "-32768", 100);
155	func_mysqli_fetch_array($mysqli, $engine, "SMALLINT", 32767, "32767", 110);
156	func_mysqli_fetch_array($mysqli, $engine, "SMALLINT", NULL, NULL, 120);
157	func_mysqli_fetch_array($mysqli, $engine, "SMALLINT UNSIGNED", 65535, "65535", 130);
158	func_mysqli_fetch_array($mysqli, $engine, "SMALLINT UNSIGNED", NULL, NULL, 140);
159
160	func_mysqli_fetch_array($mysqli, $engine, "MEDIUMINT", -8388608, "-8388608", 150);
161	func_mysqli_fetch_array($mysqli, $engine, "MEDIUMINT", 8388607, "8388607", 160);
162	func_mysqli_fetch_array($mysqli, $engine, "MEDIUMINT", NULL, NULL, 170);
163	func_mysqli_fetch_array($mysqli, $engine, "MEDIUMINT UNSIGNED", 16777215, "16777215", 180);
164	func_mysqli_fetch_array($mysqli, $engine, "MEDIUMINT UNSIGNED", NULL, NULL, 190);
165
166	func_mysqli_fetch_array($mysqli, $engine, "INTEGER", -2147483648, "-2147483648", 200);
167	func_mysqli_fetch_array($mysqli, $engine, "INTEGER", 2147483647, "2147483647", 210);
168	func_mysqli_fetch_array($mysqli, $engine, "INTEGER", NULL, NULL, 220);
169	func_mysqli_fetch_array($mysqli, $engine, "INTEGER UNSIGNED", "4294967295", "4294967295", 230);
170	func_mysqli_fetch_array($mysqli, $engine, "INTEGER UNSIGNED", NULL, NULL, 240);
171
172	if ($IS_MYSQLND ||
173		((mysqli_get_server_version($link) >= 51000) &&
174		 (mysqli_get_client_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	if (null !== ($tmp = $res->fetch_array(MYSQLI_ASSOC)))
275		printf("[015] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
276
277	print "done!";
278?>
279--CLEAN--
280<?php
281	require_once("clean_table.inc");
282?>
283--EXPECTF--
284[005]
285array(4) {
286  [0]=>
287  string(1) "1"
288  ["id"]=>
289  string(1) "1"
290  [1]=>
291  string(1) "a"
292  ["label"]=>
293  string(1) "a"
294}
295[006]
296array(2) {
297  [0]=>
298  string(1) "2"
299  [1]=>
300  string(1) "b"
301}
302[007]
303array(4) {
304  [0]=>
305  string(1) "3"
306  ["id"]=>
307  string(1) "3"
308  [1]=>
309  string(1) "c"
310  ["label"]=>
311  string(1) "c"
312}
313[008]
314array(2) {
315  ["id"]=>
316  string(1) "4"
317  ["label"]=>
318  string(1) "d"
319}
320[009]
321array(4) {
322  [0]=>
323  string(1) "5"
324  ["id"]=>
325  string(1) "5"
326  [1]=>
327  string(1) "e"
328  ["label"]=>
329  string(1) "e"
330}
331[011]
332array(11) {
333  [0]=>
334  string(1) "1"
335  ["a"]=>
336  string(1) "2"
337  [1]=>
338  string(1) "2"
339  [2]=>
340  string(1) "3"
341  ["c"]=>
342  string(1) "3"
343  [3]=>
344  string(1) "4"
345  ["C"]=>
346  string(1) "4"
347  [4]=>
348  NULL
349  ["d"]=>
350  NULL
351  [5]=>
352  string(1) "1"
353  ["e"]=>
354  string(1) "1"
355}
356
357Warning: mysqli_result::fetch_array(): The result type should be either MYSQLI_NUM, MYSQLI_ASSOC or MYSQLI_BOTH in %s on line %d
358
359Warning: mysqli_result::fetch_array(): The result type should be either MYSQLI_NUM, MYSQLI_ASSOC or MYSQLI_BOTH in %s on line %d
360
361Warning: mysqli_result::fetch_array(): Couldn't fetch mysqli_result in %s on line %d
362done!
363