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 (false !== ($tmp = mysqli_fetch_array($res, MYSQLI_ASSOC)))
300		printf("[015] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
301
302	if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
303		printf("[016] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
304			$host, $user, $db, $port, $socket);
305	}
306
307	if (!$res = mysqli_real_query($link, "SELECT 1 AS _one"))
308		printf("[017] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
309
310	/* on mysqlnd level this would not be allowed */
311	if (!is_object($res = mysqli_use_result($link)))
312		printf("[018] Expecting object, got %s/%s. [%d] %s\n",
313			gettype($res), $res, mysqli_errno($link), mysqli_error($link));
314
315	$rows = mysqli_fetch_all($res, MYSQLI_ASSOC);
316	if (!is_array($rows) || (count($rows) > 1) || !isset($rows[0]['_one']) || ($rows[0]['_one'] != 1)) {
317		printf("[019] Results seem wrong, dumping\n");
318		var_dump($rows);
319	}
320
321
322	print "done!";
323?>
324--CLEAN--
325<?php
326	// require_once("clean_table.inc");
327?>
328--EXPECTF--
329[005]
330array(2) {
331  [0]=>
332  array(2) {
333    [0]=>
334    string(1) "1"
335    [1]=>
336    string(1) "a"
337  }
338  [1]=>
339  array(2) {
340    [0]=>
341    string(1) "2"
342    [1]=>
343    string(1) "b"
344  }
345}
346[007]
347array(2) {
348  [0]=>
349  array(2) {
350    [0]=>
351    string(1) "1"
352    [1]=>
353    string(1) "a"
354  }
355  [1]=>
356  array(2) {
357    [0]=>
358    string(1) "2"
359    [1]=>
360    string(1) "b"
361  }
362}
363[008]
364array(2) {
365  [0]=>
366  array(4) {
367    [0]=>
368    string(1) "1"
369    ["id"]=>
370    string(1) "1"
371    [1]=>
372    string(1) "a"
373    ["label"]=>
374    string(1) "a"
375  }
376  [1]=>
377  array(4) {
378    [0]=>
379    string(1) "2"
380    ["id"]=>
381    string(1) "2"
382    [1]=>
383    string(1) "b"
384    ["label"]=>
385    string(1) "b"
386  }
387}
388[010]
389array(2) {
390  [0]=>
391  array(2) {
392    ["id"]=>
393    string(1) "1"
394    ["label"]=>
395    string(1) "a"
396  }
397  [1]=>
398  array(2) {
399    ["id"]=>
400    string(1) "2"
401    ["label"]=>
402    string(1) "b"
403  }
404}
405[011]
406array(0) {
407}
408[013]
409array(2) {
410  [0]=>
411  array(2) {
412    ["id"]=>
413    string(1) "1"
414    ["label"]=>
415    string(1) "a"
416  }
417  [1]=>
418  array(2) {
419    ["id"]=>
420    string(1) "2"
421    ["label"]=>
422    string(1) "b"
423  }
424}
425[016]
426array(0) {
427}
428[017]
429array(1) {
430  [0]=>
431  array(11) {
432    [0]=>
433    string(1) "1"
434    ["a"]=>
435    string(1) "2"
436    [1]=>
437    string(1) "2"
438    [2]=>
439    string(1) "3"
440    ["c"]=>
441    string(1) "3"
442    [3]=>
443    string(1) "4"
444    ["C"]=>
445    string(1) "4"
446    [4]=>
447    NULL
448    ["d"]=>
449    NULL
450    [5]=>
451    string(1) "1"
452    ["e"]=>
453    string(1) "1"
454  }
455}
456
457Warning: mysqli_fetch_all(): Mode can be only MYSQLI_FETCH_NUM, MYSQLI_FETCH_ASSOC or MYSQLI_FETCH_BOTH in %s on line %d
458
459Warning: mysqli_fetch_array(): Couldn't fetch mysqli_result in %s on line %d
460done!
461