1--TEST--
2mysqli_stmt_get_result - data types
3--SKIPIF--
4<?php
5	require_once('skipif.inc');
6	require_once('skipifemb.inc');
7	require_once('skipifconnectfailure.inc');
8
9	if (!function_exists('mysqli_stmt_get_result'))
10		die("skip mysqli_stmt_get_result() not available");
11?>
12--FILE--
13<?php
14	require('connect.inc');
15	if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
16		printf("[001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
17
18	$hint_str_or_unicode = (version_compare(PHP_VERSION, '5.9.9', '>') == 1) ? 'unicode' : 'string';
19
20	function func_mysqli_stmt_get_result($link, $engine, $bind_type, $sql_type, $bind_value, $offset, $type_hint = null) {
21
22		if (!mysqli_query($link, "DROP TABLE IF EXISTS test")) {
23			printf("[%04d] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
24			return false;
25		}
26
27		if (!mysqli_query($link, sprintf("CREATE TABLE test(id INT, label %s, PRIMARY KEY(id)) ENGINE = %s", $sql_type, $engine))) {
28			// don't bail - column type might not be supported by the server, ignore this
29			return false;
30		}
31
32		if (!$stmt = mysqli_stmt_init($link)) {
33			printf("[%04d] [%d] %s\n", $offset + 1, mysqli_errno($link), mysqli_error($link));
34			return false;
35		}
36
37		if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (?, ?)")) {
38			printf("[%04d] [%d] %s\n", $offset + 2, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
39			return false;
40		}
41
42		$id = null;
43		if (!mysqli_stmt_bind_param($stmt, "i" . $bind_type, $id, $bind_value)) {
44			printf("[%04d] [%d] %s\n", $offset + 3, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
45			mysqli_stmt_close($stmt);
46			return false;
47		}
48
49		for ($id = 1; $id < 4; $id++) {
50			if (!mysqli_stmt_execute($stmt)) {
51				printf("[%04d] [%d] %s\n", $offset + 3 + $id, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
52				mysqli_stmt_close($stmt);
53				return false;
54			}
55		}
56		mysqli_stmt_close($stmt);
57
58		$stmt = mysqli_stmt_init($link);
59
60		if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test")) {
61			printf("[%04d] [%d] %s\n", $offset + 7, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
62			mysqli_stmt_close($stmt);
63			return false;
64		}
65
66		if (!mysqli_stmt_execute($stmt)) {
67			printf("[%04d] [%d] %s\n", $offset + 8, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
68			mysqli_stmt_close($stmt);
69			return false;
70		}
71
72		$result = mysqli_stmt_result_metadata($stmt);
73
74		if (!$res = mysqli_stmt_get_result($stmt)) {
75			printf("[%04d] [%d] %s\n", $offset + 9, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
76			mysqli_stmt_close($stmt);
77			return false;
78		}
79		$num = 0;
80		$fields = mysqli_fetch_fields($result);
81
82		while ($row = mysqli_fetch_assoc($res)) {
83			$bind_res = &$row['label'];
84			if (!gettype($bind_res) == 'unicode') {
85				if ($bind_res !== $bind_value && (!$type_hint || ($type_hint !== gettype($bind_res)))) {
86					printf("[%04d] [%d] Expecting %s/'%s' [type hint = %s], got %s/'%s'\n",
87						$offset + 10, $num,
88						gettype($bind_value), $bind_value, $type_hint,
89						gettype($bind_res), $bind_res);
90						mysqli_free_result($res);
91						mysqli_stmt_close($stmt);
92						return false;
93				}
94			}
95			$num++;
96		}
97
98		if ($num != 3) {
99			printf("[%04d] [%d] %s, expecting 3 results, got only %d results\n",
100				$offset + 11, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt), $num);
101			mysqli_free_result($res);
102			mysqli_stmt_close($stmt);
103			return false;
104		}
105
106		mysqli_free_result($res);
107		mysqli_stmt_close($stmt);
108		return true;
109	}
110
111
112	function func_mysqli_stmt_bind_make_string($len) {
113
114		$ret = '';
115		for ($i = 0; $i < $len; $i++)
116			$ret .= chr(mt_rand(65, 90));
117
118		return $ret;
119	}
120
121	func_mysqli_stmt_get_result($link, $engine, "i", "TINYINT", -11, 20);
122	func_mysqli_stmt_get_result($link, $engine, "i", "TINYINT", NULL, 40);
123	func_mysqli_stmt_get_result($link, $engine, "i", "TINYINT UNSIGNED", 1, 60);
124	func_mysqli_stmt_get_result($link, $engine, "i", "TINYINT UNSIGNED", NULL, 80);
125
126	func_mysqli_stmt_get_result($link, $engine, "i", "BOOL", 1, 100);
127	func_mysqli_stmt_get_result($link, $engine, "i", "BOOL", NULL, 120);
128	func_mysqli_stmt_get_result($link, $engine, "i", "BOOLEAN", 0, 140);
129	func_mysqli_stmt_get_result($link, $engine, "i", "BOOLEAN", NULL, 160);
130
131	func_mysqli_stmt_get_result($link, $engine, "i", "SMALLINT", -32768, 180);
132	func_mysqli_stmt_get_result($link, $engine, "i", "SMALLINT", 32767, 200);
133	func_mysqli_stmt_get_result($link, $engine, "i", "SMALLINT", NULL, 220);
134	func_mysqli_stmt_get_result($link, $engine, "i", "SMALLINT UNSIGNED", 65535, 240);
135	func_mysqli_stmt_get_result($link, $engine, "i", "SMALLINT UNSIGNED", NULL, 260);
136
137	func_mysqli_stmt_get_result($link, $engine, "d", "MEDIUMINT", -8388608, 280, "integer");
138	func_mysqli_stmt_get_result($link, $engine, "d", "MEDIUMINT", 8388607, 300, "integer");
139	func_mysqli_stmt_get_result($link, $engine, "d", "MEDIUMINT", NULL, 320);
140	func_mysqli_stmt_get_result($link, $engine, "d", "MEDIUMINT UNSIGNED", 16777215, 340, "integer");
141	func_mysqli_stmt_get_result($link, $engine, "d", "MEDIUMINT UNSIGNED", NULL, 360);
142
143	func_mysqli_stmt_get_result($link, $engine, "i", "INTEGER", (defined("PHP_INT_MAX")) ? max(-1 * PHP_INT_MAX + 1, -2147483648) : 1, 380);
144	func_mysqli_stmt_get_result($link, $engine, "i", "INTEGER", -2147483647, 400, "integer");
145	func_mysqli_stmt_get_result($link, $engine, "i", "INTEGER", (defined("PHP_INT_MAX")) ? min(2147483647, PHP_INT_MAX) : 1, 420);
146	func_mysqli_stmt_get_result($link, $engine, "i", "INTEGER", NULL, 440);
147	func_mysqli_stmt_get_result($link, $engine, "i", "INTEGER UNSIGNED", (defined("PHP_INT_MAX")) ? min(4294967295, 2147483647) : 1, 460);
148	func_mysqli_stmt_get_result($link, $engine, "i", "INTEGER UNSIGNED", 4294967295, 480, (defined("PHP_INT_MAX") && (4294967295 > PHP_INT_MAX)) ? "string" : null);
149	func_mysqli_stmt_get_result($link, $engine, "i", "INTEGER UNSIGNED", NULL, 500);
150
151	/* test is broken too: we bind "integer" but value is a float
152	func_mysqli_stmt_get_result($link, $engine, "i", "BIGINT", -9223372036854775808, 520);
153	func_mysqli_stmt_get_result($link, $engine, "i", "BIGINT UNSIGNED", 18446744073709551615, 560);
154	*/
155	func_mysqli_stmt_get_result($link, $engine, "i", "BIGINT", NULL, 540);
156	func_mysqli_stmt_get_result($link, $engine, "i", "BIGINT UNSIGNED", NULL, 580);
157	func_mysqli_stmt_get_result($link, $engine, "i", "BIGINT", -1, 1780);
158	func_mysqli_stmt_get_result($link, $engine, "i", "BIGINT UNSIGNED", 1, 1800);
159	func_mysqli_stmt_get_result($link, $engine, "i", "BIGINT", -1 * PHP_INT_MAX + 1, 1820);
160	func_mysqli_stmt_get_result($link, $engine, "i", "BIGINT UNSIGNED", PHP_INT_MAX, 1840);
161	func_mysqli_stmt_get_result($link, $engine, "s", "BIGINT UNSIGNED", "18446744073709551615", 1860);
162	func_mysqli_stmt_get_result($link, $engine, "s", "BIGINT", "-9223372036854775808", 1880);
163
164	func_mysqli_stmt_get_result($link, $engine, "d", "FLOAT", -9223372036854775808 - 1.1, 600);
165	func_mysqli_stmt_get_result($link, $engine, "d", "FLOAT", NULL, 620);
166	func_mysqli_stmt_get_result($link, $engine, "d", "FLOAT UNSIGNED", 18446744073709551615 + 1.1, 640);
167	func_mysqli_stmt_get_result($link, $engine, "d", "FLOAT UNSIGNED ", NULL, 660);
168
169	// Yes, we need the temporary variable. The PHP casting will fouls us otherwise.
170	$tmp = strval('-99999999.99');
171	func_mysqli_stmt_get_result($link, $engine, "d", "DOUBLE(10,2)", $tmp, 680, "string");
172	func_mysqli_stmt_get_result($link, $engine, "d", "DOUBLE(10,2)", NULL, 700);
173	$tmp = strval('99999999.99');
174	func_mysqli_stmt_get_result($link, $engine, "d", "DOUBLE(10,2) UNSIGNED", $tmp , 720, "string");
175	func_mysqli_stmt_get_result($link, $engine, "d", "DOUBLE(10,2) UNSIGNED", NULL, 740);
176	$tmp = strval('-99999999.99');
177	func_mysqli_stmt_get_result($link, $engine, "d", "DECIMAL(10,2)", $tmp, 760, "string");
178	func_mysqli_stmt_get_result($link, $engine, "d", "DECIMAL(10,2)", NULL, 780);
179	$tmp = strval('99999999.99');
180	func_mysqli_stmt_get_result($link, $engine, "d", "DECIMAL(10,2)", $tmp, 800, "string");
181	func_mysqli_stmt_get_result($link, $engine, "d", "DECIMAL(10,2)", NULL, 820);
182
183	// don't care about date() strict TZ warnings...
184	func_mysqli_stmt_get_result($link, $engine, "s", "DATE", @date('Y-m-d'), 840);
185	func_mysqli_stmt_get_result($link, $engine, "s", "DATE NOT NULL", @date('Y-m-d'), 860);
186	func_mysqli_stmt_get_result($link, $engine, "s", "DATE", NULL, 880);
187
188	func_mysqli_stmt_get_result($link, $engine, "s", "DATETIME", @date('Y-m-d H:i:s'), 900);
189	func_mysqli_stmt_get_result($link, $engine, "s", "DATETIME NOT NULL", @date('Y-m-d H:i:s'), 920);
190	func_mysqli_stmt_get_result($link, $engine, "s", "DATETIME", NULL, 940);
191
192	func_mysqli_stmt_get_result($link, $engine, "s", "TIMESTAMP", @date('Y-m-d H:i:s'), 960);
193
194	func_mysqli_stmt_get_result($link, $engine, "s", "TIME", @date('H:i:s'), 980);
195	func_mysqli_stmt_get_result($link, $engine, "s", "TIME NOT NULL", @date('H:i:s'), 1000);
196	func_mysqli_stmt_get_result($link, $engine, "s", "TIME", NULL, 1020);
197
198	$tmp = intval(@date('Y'));
199	func_mysqli_stmt_get_result($link, $engine, "s", "YEAR", $tmp, 1040, "integer");
200	func_mysqli_stmt_get_result($link, $engine, "s", "YEAR NOT NULL", $tmp, 1060, "integer");
201	func_mysqli_stmt_get_result($link, $engine, "s", "YEAR", NULL, 1080);
202
203	$string255 = func_mysqli_stmt_bind_make_string(255);
204	func_mysqli_stmt_get_result($link, $engine, "s", "CHAR(1)", "a", 1110, $hint_str_or_unicode);
205	func_mysqli_stmt_get_result($link, $engine, "s", "CHAR(255)", $string255, 1120, $hint_str_or_unicode);
206	func_mysqli_stmt_get_result($link, $engine, "s", "CHAR(1) NOT NULL", "a", 1140, $hint_str_or_unicode);
207	func_mysqli_stmt_get_result($link, $engine, "s", "CHAR(1)", NULL, 1160);
208
209	$string65k = func_mysqli_stmt_bind_make_string(65535);
210	func_mysqli_stmt_get_result($link, $engine, "s", "VARCHAR(1)", "a", 1180, $hint_str_or_unicode);
211	func_mysqli_stmt_get_result($link, $engine, "s", "VARCHAR(255)", $string255, 1200, $hint_str_or_unicode);
212	func_mysqli_stmt_get_result($link, $engine, "s", "VARCHAR(65635)", $string65k, 1220, $hint_str_or_unicode);
213	func_mysqli_stmt_get_result($link, $engine, "s", "VARCHAR(1) NOT NULL", "a", 1240, $hint_str_or_unicode);
214	func_mysqli_stmt_get_result($link, $engine, "s", "VARCHAR(1)", NULL, 1260);
215
216	func_mysqli_stmt_get_result($link, $engine, "s", "BINARY(1)", "a", 1280);
217	func_mysqli_stmt_get_result($link, $engine, "s", "BINARY(1)", chr(0), 1300);
218	func_mysqli_stmt_get_result($link, $engine, "s", "BINARY(1) NOT NULL", "b", 1320);
219	func_mysqli_stmt_get_result($link, $engine, "s", "BINARY(1)", NULL, 1340);
220
221	func_mysqli_stmt_get_result($link, $engine, "s", "VARBINARY(1)", "a", 1360);
222	func_mysqli_stmt_get_result($link, $engine, "s", "VARBINARY(1)", chr(0), 1380);
223	func_mysqli_stmt_get_result($link, $engine, "s", "VARBINARY(1) NOT NULL", "b", 1400);
224	func_mysqli_stmt_get_result($link, $engine, "s", "VARBINARY(1)", NULL, 1420);
225
226	func_mysqli_stmt_get_result($link, $engine, "s", "TINYBLOB", "a", 1440);
227	func_mysqli_stmt_get_result($link, $engine, "s", "TINYBLOB", chr(0), 1460);
228	func_mysqli_stmt_get_result($link, $engine, "s", "TINYBLOB NOT NULL", "b", 1480);
229	func_mysqli_stmt_get_result($link, $engine, "s", "TINYBLOB", NULL, 1500);
230
231	func_mysqli_stmt_get_result($link, $engine, "s", "TINYTEXT", "a", 1520, $hint_str_or_unicode);
232	func_mysqli_stmt_get_result($link, $engine, "s", "TINYTEXT NOT NULL", "a", 1540, $hint_str_or_unicode);
233	func_mysqli_stmt_get_result($link, $engine, "s", "TINYTEXT", NULL, 1560, $hint_str_or_unicode);
234
235	// Note: you cannot insert any blob values this way. But you can check the API at least partly this way
236	// Extra BLOB tests are in mysqli_stmt_send_long()
237	func_mysqli_stmt_get_result($link, $engine, "b", "BLOB", b"", 1580);
238	func_mysqli_stmt_get_result($link, $engine, "b", "TEXT", "", 1600, $hint_str_or_unicode);
239	func_mysqli_stmt_get_result($link, $engine, "b", "MEDIUMBLOB", b"", 1620);
240	func_mysqli_stmt_get_result($link, $engine, "b", "MEDIUMTEXT", "", 1640, $hint_str_or_unicode);
241
242	/* Is this one related? http://bugs.php.net/bug.php?id=35759 */
243	func_mysqli_stmt_get_result($link, $engine, "b", "LONGBLOB", "", 1660);
244	func_mysqli_stmt_get_result($link, $engine, "b", "LONGTEXT", "", 1680, $hint_str_or_unicode);
245
246	func_mysqli_stmt_get_result($link, $engine, "s", "ENUM('a', 'b')", "a", 1700, $hint_str_or_unicode);
247	func_mysqli_stmt_get_result($link, $engine, "s", "ENUM('a', 'b')", NULL, 1720, $hint_str_or_unicode);
248	func_mysqli_stmt_get_result($link, $engine, "s", "SET('a', 'b')", "a", 1740, $hint_str_or_unicode);
249	func_mysqli_stmt_get_result($link, $engine, "s", "SET('a', 'b')", NULL, 1760, $hint_str_or_unicode);
250
251	mysqli_close($link);
252	print "done!";
253?>
254--CLEAN--
255<?php
256	require_once("clean_table.inc");
257?>
258--EXPECTF--
259done!