1--TEST--
2mysqli_stmt_bind_result()
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_stmt_bind_result()))
17		printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
18
19	if (!is_null($tmp = @mysqli_stmt_bind_result($link)))
20		printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
21
22	require('table.inc');
23
24	$stmt = mysqli_stmt_init($link);
25	if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test ORDER BY id LIMIT 1"))
26		printf("[002a] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
27
28	if (!is_null($tmp = mysqli_stmt_bind_result($stmt)))
29		printf("[002b] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
30
31	mysqli_stmt_close($stmt);
32	$stmt = mysqli_stmt_init($link);
33
34	$id = null;
35	$label = null;
36	$foo = null;
37
38	if (!is_null($tmp = mysqli_stmt_bind_result($stmt, $id)))
39		printf("[003] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
40
41	if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test ORDER BY id LIMIT 1"))
42		printf("[004] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
43
44	if (false !== ($tmp = mysqli_stmt_bind_result($stmt, $id)))
45		printf("[005] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
46
47	if (true !== ($tmp = mysqli_stmt_bind_result($stmt, $id, $label)))
48		printf("[006] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
49
50	if (false !== ($tmp = mysqli_stmt_bind_result($stmt, $id, $label, $foo)))
51		printf("[007] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
52
53	if (!mysqli_stmt_execute($stmt))
54		printf("[008] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
55
56	while (mysqli_stmt_fetch($stmt)) {
57		var_dump($id);
58		var_dump($label);
59	}
60	mysqli_stmt_close($stmt);
61
62
63	function func_mysqli_stmt_bind_result($link, $engine, $bind_type, $sql_type, $bind_value, $offset, $type_hint = null) {
64
65		if (!mysqli_query($link, "DROP TABLE IF EXISTS test")) {
66			printf("[%04d] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
67			return false;
68		}
69
70		if (!mysqli_query($link, sprintf("CREATE TABLE test(id INT, label %s, PRIMARY KEY(id)) ENGINE = %s", $sql_type, $engine))) {
71			// don't bail - column type might not be supported by the server, ignore this
72			return false;
73		}
74
75		if (!$stmt = mysqli_stmt_init($link)) {
76			printf("[%04d] [%d] %s\n", $offset + 1, mysqli_errno($link), mysqli_error($link));
77			return false;
78		}
79
80		if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (?, ?)")) {
81			printf("[%04d] [%d] %s\n", $offset + 2, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
82			return false;
83		}
84
85		$id = null;
86		if (!mysqli_stmt_bind_param($stmt, "i" . $bind_type, $id, $bind_value)) {
87			printf("[%04d] [%d] %s\n", $offset + 3, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
88			mysqli_stmt_close($stmt);
89			return false;
90		}
91
92		for ($id = 1; $id < 4; $id++) {
93			if (!mysqli_stmt_execute($stmt)) {
94				printf("[%04d] [%d] %s\n", $offset + 3 + $id, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
95				mysqli_stmt_close($stmt);
96				return false;
97			}
98		}
99		mysqli_stmt_close($stmt);
100
101		$stmt = mysqli_stmt_init($link);
102
103		if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test")) {
104			printf("[%04d] [%d] %s\n", $offset + 7, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
105			mysqli_stmt_close($stmt);
106			return false;
107		}
108
109		if (!mysqli_stmt_execute($stmt)) {
110			printf("[%04d] [%d] %s\n", $offset + 8, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
111			mysqli_stmt_close($stmt);
112			return false;
113		}
114
115		$result = mysqli_stmt_result_metadata($stmt);
116
117		$bind_res = null;
118		if (!mysqli_stmt_bind_result($stmt, $id, $bind_res)) {
119			printf("[%04d] [%d] %s\n", $offset + 9, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
120			mysqli_stmt_close($stmt);
121			return false;
122		}
123		$num = 0;
124		$fields = mysqli_fetch_fields($result);
125
126		while (mysqli_stmt_fetch($stmt)) {
127			if (!gettype($bind_res)=="unicode") {
128				if ($bind_res !== $bind_value && (!$type_hint || ($type_hint !== gettype($bind_res)))) {
129					printf("[%04d] [%d] Expecting %s/'%s' [type hint = %s], got %s/'%s'\n",
130						$offset + 10, $num,
131						gettype($bind_value), $bind_value, $type_hint,
132						gettype($bind_res), $bind_res);
133						mysqli_stmt_close($stmt);
134						return false;
135				}
136			}
137			$num++;
138		}
139
140		if ($num != 3) {
141			printf("[%04d] [%d] %s, expecting 3 results, got only %d results\n",
142				$offset + 11, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt), $num);
143			mysqli_stmt_close($stmt);
144			return false;
145		}
146
147		mysqli_stmt_close($stmt);
148		return true;
149	}
150
151
152	function func_mysqli_stmt_bind_make_string($len) {
153
154		$ret = '';
155		for ($i = 0; $i < $len; $i++)
156			$ret .= chr(mt_rand(65, 90));
157
158		return $ret;
159	}
160
161	func_mysqli_stmt_bind_result($link, $engine, "i", "TINYINT", -11, 20);
162	func_mysqli_stmt_bind_result($link, $engine, "i", "TINYINT", NULL, 40);
163	func_mysqli_stmt_bind_result($link, $engine, "i", "TINYINT UNSIGNED", 1, 60);
164	func_mysqli_stmt_bind_result($link, $engine, "i", "TINYINT UNSIGNED", NULL, 80);
165
166	func_mysqli_stmt_bind_result($link, $engine, "i", "BOOL", 1, 100);
167	func_mysqli_stmt_bind_result($link, $engine, "i", "BOOL", NULL, 120);
168	func_mysqli_stmt_bind_result($link, $engine, "i", "BOOLEAN", 0, 140);
169	func_mysqli_stmt_bind_result($link, $engine, "i", "BOOLEAN", NULL, 160);
170
171	func_mysqli_stmt_bind_result($link, $engine, "i", "SMALLINT", -32768, 180);
172	func_mysqli_stmt_bind_result($link, $engine, "i", "SMALLINT", 32767, 200);
173	func_mysqli_stmt_bind_result($link, $engine, "i", "SMALLINT", NULL, 220);
174	func_mysqli_stmt_bind_result($link, $engine, "i", "SMALLINT UNSIGNED", 65535, 240);
175	func_mysqli_stmt_bind_result($link, $engine, "i", "SMALLINT UNSIGNED", NULL, 260);
176
177	func_mysqli_stmt_bind_result($link, $engine, "d", "MEDIUMINT", -8388608, 280, "integer");
178	func_mysqli_stmt_bind_result($link, $engine, "d", "MEDIUMINT", 8388607, 300, "integer");
179	func_mysqli_stmt_bind_result($link, $engine, "d", "MEDIUMINT", NULL, 320);
180	func_mysqli_stmt_bind_result($link, $engine, "d", "MEDIUMINT UNSIGNED", 16777215, 340, "integer");
181	func_mysqli_stmt_bind_result($link, $engine, "d", "MEDIUMINT UNSIGNED", NULL, 360);
182
183	func_mysqli_stmt_bind_result($link, $engine, "i", "INTEGER", (defined("PHP_INT_MAX")) ? max(-1 * PHP_INT_MAX + 1, -2147483648) : 1, 380);
184	func_mysqli_stmt_bind_result($link, $engine, "i", "INTEGER", -2147483647, 400, "integer");
185	func_mysqli_stmt_bind_result($link, $engine, "i", "INTEGER", (defined("PHP_INT_MAX")) ? min(2147483647, PHP_INT_MAX) : 1, 420);
186	func_mysqli_stmt_bind_result($link, $engine, "i", "INTEGER", NULL, 440);
187	func_mysqli_stmt_bind_result($link, $engine, "i", "INTEGER UNSIGNED", (defined("PHP_INT_MAX")) ? min(4294967295, 2147483647) : 1, 460);
188	func_mysqli_stmt_bind_result($link, $engine, "i", "INTEGER UNSIGNED", 4294967295, 480, (defined("PHP_INT_MAX") && (4294967295 > PHP_INT_MAX)) ? "string" : null);
189	func_mysqli_stmt_bind_result($link, $engine, "i", "INTEGER UNSIGNED", NULL, 500);
190
191	/* test is broken too: we bind "integer" but value is a float
192	func_mysqli_stmt_bind_result($link, $engine, "i", "BIGINT", -9223372036854775808, 520);
193	func_mysqli_stmt_bind_result($link, $engine, "i", "BIGINT UNSIGNED", 18446744073709551615, 560);
194	*/
195	func_mysqli_stmt_bind_result($link, $engine, "i", "BIGINT", NULL, 540);
196	func_mysqli_stmt_bind_result($link, $engine, "i", "BIGINT UNSIGNED", NULL, 580);
197	func_mysqli_stmt_bind_result($link, $engine, "i", "BIGINT", -1, 1780);
198	func_mysqli_stmt_bind_result($link, $engine, "i", "BIGINT UNSIGNED", 1, 1800);
199	func_mysqli_stmt_bind_result($link, $engine, "i", "BIGINT", -1 * PHP_INT_MAX + 1, 1820);
200	func_mysqli_stmt_bind_result($link, $engine, "i", "BIGINT UNSIGNED", PHP_INT_MAX, 1840);
201	func_mysqli_stmt_bind_result($link, $engine, "s", "BIGINT UNSIGNED", "18446744073709551615", 1860);
202	func_mysqli_stmt_bind_result($link, $engine, "s", "BIGINT", "-9223372036854775808", 1880);
203
204	func_mysqli_stmt_bind_result($link, $engine, "d", "FLOAT", -9223372036854775808 - 1.1, 600);
205	func_mysqli_stmt_bind_result($link, $engine, "d", "FLOAT", NULL, 620);
206	func_mysqli_stmt_bind_result($link, $engine, "d", "FLOAT UNSIGNED", 18446744073709551615 + 1.1, 640);
207	func_mysqli_stmt_bind_result($link, $engine, "d", "FLOAT UNSIGNED ", NULL, 660);
208
209	// Yes, we need the temporary variable. The PHP casting will fouls us otherwise.
210	$tmp = strval('-99999999.99');
211	func_mysqli_stmt_bind_result($link, $engine, "d", "DOUBLE(10,2)", $tmp, 680, "string");
212	func_mysqli_stmt_bind_result($link, $engine, "d", "DOUBLE(10,2)", NULL, 700);
213	$tmp = strval('99999999.99');
214	func_mysqli_stmt_bind_result($link, $engine, "d", "DOUBLE(10,2) UNSIGNED", $tmp , 720, "string");
215	func_mysqli_stmt_bind_result($link, $engine, "d", "DOUBLE(10,2) UNSIGNED", NULL, 740);
216	$tmp = strval('-99999999.99');
217	func_mysqli_stmt_bind_result($link, $engine, "d", "DECIMAL(10,2)", $tmp, 760, "string");
218	func_mysqli_stmt_bind_result($link, $engine, "d", "DECIMAL(10,2)", NULL, 780);
219	$tmp = strval('99999999.99');
220	func_mysqli_stmt_bind_result($link, $engine, "d", "DECIMAL(10,2)", $tmp, 800, "string");
221	func_mysqli_stmt_bind_result($link, $engine, "d", "DECIMAL(10,2)", NULL, 820);
222
223	// don't care about date() strict TZ warnings...
224	func_mysqli_stmt_bind_result($link, $engine, "s", "DATE", @date('Y-m-d'), 840);
225	func_mysqli_stmt_bind_result($link, $engine, "s", "DATE NOT NULL", @date('Y-m-d'), 860);
226	func_mysqli_stmt_bind_result($link, $engine, "s", "DATE", NULL, 880);
227
228	func_mysqli_stmt_bind_result($link, $engine, "s", "DATETIME", @date('Y-m-d H:i:s'), 900);
229	func_mysqli_stmt_bind_result($link, $engine, "s", "DATETIME NOT NULL", @date('Y-m-d H:i:s'), 920);
230	func_mysqli_stmt_bind_result($link, $engine, "s", "DATETIME", NULL, 940);
231
232	func_mysqli_stmt_bind_result($link, $engine, "s", "TIMESTAMP", @date('Y-m-d H:i:s'), 960);
233
234	func_mysqli_stmt_bind_result($link, $engine, "s", "TIME", @date('H:i:s'), 980);
235	func_mysqli_stmt_bind_result($link, $engine, "s", "TIME NOT NULL", @date('H:i:s'), 1000);
236	func_mysqli_stmt_bind_result($link, $engine, "s", "TIME", NULL, 1020);
237
238	$tmp = intval(@date('Y'));
239	func_mysqli_stmt_bind_result($link, $engine, "s", "YEAR", $tmp, 1040, "integer");
240	func_mysqli_stmt_bind_result($link, $engine, "s", "YEAR NOT NULL", $tmp, 1060, "integer");
241	func_mysqli_stmt_bind_result($link, $engine, "s", "YEAR", NULL, 1080);
242
243	$string255 = func_mysqli_stmt_bind_make_string(255);
244	func_mysqli_stmt_bind_result($link, $engine, "s", "CHAR(1)", "a", 1110, 'string');
245	func_mysqli_stmt_bind_result($link, $engine, "s", "CHAR(255)", $string255, 1120, 'string');
246	func_mysqli_stmt_bind_result($link, $engine, "s", "CHAR(1) NOT NULL", "a", 1140, 'string');
247	func_mysqli_stmt_bind_result($link, $engine, "s", "CHAR(1)", NULL, 1160);
248
249	$string65k = func_mysqli_stmt_bind_make_string(65535);
250	func_mysqli_stmt_bind_result($link, $engine, "s", "VARCHAR(1)", "a", 1180, 'string');
251	func_mysqli_stmt_bind_result($link, $engine, "s", "VARCHAR(255)", $string255, 1200, 'string');
252	func_mysqli_stmt_bind_result($link, $engine, "s", "VARCHAR(65635)", $string65k, 1220, 'string');
253	func_mysqli_stmt_bind_result($link, $engine, "s", "VARCHAR(1) NOT NULL", "a", 1240, 'string');
254	func_mysqli_stmt_bind_result($link, $engine, "s", "VARCHAR(1)", NULL, 1260);
255
256	func_mysqli_stmt_bind_result($link, $engine, "s", "BINARY(1)", "a", 1280);
257	func_mysqli_stmt_bind_result($link, $engine, "s", "BINARY(1)", chr(0), 1300);
258	func_mysqli_stmt_bind_result($link, $engine, "s", "BINARY(1) NOT NULL", "b", 1320);
259	func_mysqli_stmt_bind_result($link, $engine, "s", "BINARY(1)", NULL, 1340);
260
261	func_mysqli_stmt_bind_result($link, $engine, "s", "VARBINARY(1)", "a", 1360);
262	func_mysqli_stmt_bind_result($link, $engine, "s", "VARBINARY(1)", chr(0), 1380);
263	func_mysqli_stmt_bind_result($link, $engine, "s", "VARBINARY(1) NOT NULL", "b", 1400);
264	func_mysqli_stmt_bind_result($link, $engine, "s", "VARBINARY(1)", NULL, 1420);
265
266	func_mysqli_stmt_bind_result($link, $engine, "s", "TINYBLOB", "a", 1440);
267	func_mysqli_stmt_bind_result($link, $engine, "s", "TINYBLOB", chr(0), 1460);
268	func_mysqli_stmt_bind_result($link, $engine, "s", "TINYBLOB NOT NULL", "b", 1480);
269	func_mysqli_stmt_bind_result($link, $engine, "s", "TINYBLOB", NULL, 1500);
270
271	func_mysqli_stmt_bind_result($link, $engine, "s", "TINYTEXT", "a", 1520, 'string');
272	func_mysqli_stmt_bind_result($link, $engine, "s", "TINYTEXT NOT NULL", "a", 1540, 'string');
273	func_mysqli_stmt_bind_result($link, $engine, "s", "TINYTEXT", NULL, 1560, 'string');
274
275	// Note: you cannot insert any blob values this way. But you can check the API at least partly this way
276	// Extra BLOB tests are in mysqli_stmt_send_long()
277	func_mysqli_stmt_bind_result($link, $engine, "b", "BLOB", "", 1580);
278	func_mysqli_stmt_bind_result($link, $engine, "b", "TEXT", "", 1600, 'string');
279	func_mysqli_stmt_bind_result($link, $engine, "b", "MEDIUMBLOB", "", 1620);
280	func_mysqli_stmt_bind_result($link, $engine, "b", "MEDIUMTEXT", "", 1640, 'string');
281
282	/* Is this one related? http://bugs.php.net/bug.php?id=35759 */
283	if (($IS_MYSQLND) || (!$IS_MYSQLND && (ini_get('memory_limit') > 4294967296))) {
284		/* NOTE: the MySQL Client Library - not mysqlnd - will allocate
285		a hugge max_length(type) = 4GB bind buffer */
286		func_mysqli_stmt_bind_result($link, $engine, "b", "LONGBLOB", "", 1660);
287		func_mysqli_stmt_bind_result($link, $engine, "b", "LONGTEXT", "", 1680, 'string');
288	}
289
290	func_mysqli_stmt_bind_result($link, $engine, "s", "ENUM('a', 'b')", "a", 1700, 'string');
291	func_mysqli_stmt_bind_result($link, $engine, "s", "ENUM('a', 'b')", NULL, 1720, 'string');
292	func_mysqli_stmt_bind_result($link, $engine, "s", "SET('a', 'b')", "a", 1740, 'string');
293	func_mysqli_stmt_bind_result($link, $engine, "s", "SET('a', 'b')", NULL, 1760, 'string');
294
295	if (mysqli_get_server_version($link) >= 50600)
296		func_mysqli_stmt_bind_result($link, $engine, "s", "TIME", "13:31:34.123456", 1770, "13:31:34");
297
298	/* Check that the function alias exists. It's a deprecated function,
299	but we have not announce the removal so far, therefore we need to check for it */
300	if (!is_null($tmp = @mysqli_stmt_bind_result()))
301		printf("[3000] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
302
303	$stmt = mysqli_stmt_init($link);
304	if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (1000, 'z')"))
305		printf("[3001] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
306
307	$id = null;
308	if (false !== @mysqli_stmt_bind_result($stmt, $id))
309		printf("[3002] Bind result should not be allowed");
310
311	mysqli_stmt_close($stmt);
312
313	mysqli_close($link);
314	print "done!";
315?>
316--CLEAN--
317<?php
318	require_once("clean_table.inc");
319?>
320--EXPECTF--
321Warning: mysqli_stmt_bind_result() expects at least 2 parameters, 1 given in %s on line %d
322
323Warning: mysqli_stmt_bind_result(): invalid object or resource mysqli_stmt
324 in %s on line %d
325
326Warning: mysqli_stmt_bind_result(): Number of bind variables doesn't match number of fields in prepared statement in %s on line %d
327
328Warning: mysqli_stmt_bind_result(): Number of bind variables doesn't match number of fields in prepared statement in %s on line %d
329int(1)
330%s(1) "a"
331done!
332