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