1--TEST--
2mysqli_multi_query()
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	if (!is_null($tmp = @mysqli_multi_query()))
17		printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
18
19	if (!is_null($tmp = @mysqli_multi_query($link)))
20		printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
21
22	require('table.inc');
23
24	if (false !== ($tmp = mysqli_multi_query($link, "")))
25		printf("[003] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
26
27	if (!mysqli_multi_query($link, "SELECT 1 AS a; SELECT 1 AS a, 2 AS b; SELECT id FROM test ORDER BY id LIMIT 3"))
28		printf("[005] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
29
30	$i = 0;
31	do {
32		$res = mysqli_store_result($link);
33		while ($row = mysqli_fetch_array($res))
34			;
35		mysqli_free_result($res);
36		$i++;
37	} while (mysqli_next_result($link));
38
39	printf("[006] %d\n", $i);
40
41	if (!mysqli_multi_query($link, "ALTER TABLE test MODIFY id INT AUTO_INCREMENT; INSERT INTO test(label) VALUES ('a'); SELECT id, label FROM test ORDER BY id"))
42		printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
43
44	$i = 0;
45	while (mysqli_next_result($link) && ($res = mysqli_store_result($link))) {
46
47		while ($row = mysqli_fetch_array($res))
48			;
49		mysqli_free_result($res);
50		printf("%d/%d\n", $i, mysqli_insert_id($link));
51		$i++;
52	}
53	printf("[008] %d\n", $i);
54
55	if (!mysqli_multi_query($link, "SELECT id, label FROM test"))
56		printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
57
58	$i = 0;
59	while (mysqli_next_result($link) && ($res = mysqli_store_result($link))) {
60		while ($row = mysqli_fetch_array($res))
61			$i++;
62		mysqli_free_result($res);
63	}
64	printf("[010] %d\n", $i);
65
66	if (!mysqli_multi_query($link, "SELECT 1 AS num, 'a' AS somechar; SELECT 2 AS num, 'a' AS somechar; SELECT 3 AS num, 'a' AS somechar"))
67		printf("[011] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
68
69	$res_num = 1;
70	do {
71		if (!$res = mysqli_store_result($link)) {
72			printf("[012 - %d] [%d] %s\n", $res_num, mysqli_errno($link), mysqli_error($link));
73			continue;
74		}
75
76		$num_rows = 0;
77		while ($row = mysqli_fetch_array($res)) {
78
79			$num_rows++;
80			if ($row['num'] != $res_num)
81				printf("[013 - %d] Expecting %s got %s\n", $res_num, $res_num, $row['num']);
82			if ($row['somechar'] != "a")
83				printf("[014 - %d] Expecting a got %s\n", $res_num, $row['somechar']);
84
85			if (1 == $num_rows) {
86				/* simple metadata check */
87				if (!($lengths = mysqli_fetch_lengths($res)))
88					printf("[015 - %d] [%d] %s\n", $res_num, mysqli_errno($link), mysqli_error($link));
89
90				if (count($lengths) != 2)
91					printf("[016 - %d] Expecting 2 column lengths got %d [%d] %s\n", $res_num, count($lengths));
92
93				foreach ($lengths as $k => $length)
94					if ($length <= 0)
95						printf("[017 - %d] Strange column lengths for column %d, got %d expecting any > 0\n",
96							$res_num, $k, $length);
97				}
98		}
99
100		if ($num_rows != 1)
101			printf("[018 - %d] Expecting 1 row, got %d rows\n", $num_rows);
102
103		$res_num++;
104
105		mysqli_free_result($res);
106
107	} while (@mysqli_next_result($link));
108
109	if ($res_num != 4)
110		printf("[015] Expecting 3 result sets got %d result set[s]\n", $res_num);
111
112	mysqli_close($link);
113
114	var_dump(mysqli_multi_query($link, "SELECT id, label FROM test"));
115
116	print "done!";
117?>
118--CLEAN--
119<?php
120	require_once("clean_table.inc");
121?>
122--EXPECTF--
123Strict Standards: mysqli_next_result(): There is no next result set. Please, call mysqli_more_results()/mysqli::more_results() to check whether to call this function/method in %s on line %d
124[006] 3
125[008] 0
126[009] [2014] Commands out of sync; you can't run this command now
127
128Strict Standards: mysqli_next_result(): There is no next result set. Please, call mysqli_more_results()/mysqli::more_results() to check whether to call this function/method in %s on line %d
129[010] 7
130
131Warning: mysqli_multi_query(): Couldn't fetch mysqli in %s on line %d
132bool(false)
133done!
134