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