1--TEST--
2mysqli_stmt_get_result() - meta data
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
7require_once('skipifconnectfailure.inc');
8
9if (!function_exists('mysqli_stmt_get_result'))
10	die('skip mysqli_stmt_get_result not available');
11?>
12--FILE--
13<?php
14	require('table.inc');
15
16	if (!$stmt = mysqli_stmt_init($link))
17		printf("[001] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
18
19	if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test ORDER BY id ASC LIMIT 3"))
20		printf("[002] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
21
22	if (!mysqli_stmt_execute($stmt))
23		printf("[003] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
24
25	if (!is_object($res = mysqli_stmt_get_result($stmt)) || 'mysqli_result' != get_class($res)) {
26		printf("[004] Expecting object/mysqli_result got %s/%s, [%d] %s\n",
27			gettype($res), $res, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
28	}
29
30	if (!is_object($res_meta = mysqli_stmt_result_metadata($stmt)) ||
31		'mysqli_result' != get_class($res_meta)) {
32		printf("[005] Expecting object/mysqli_result got %s/%s, [%d] %s\n",
33			gettype($res), $res, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
34	}
35
36	var_dump(mysqli_fetch_assoc($res));
37	var_dump(mysqli_fetch_assoc($res_meta));
38
39	mysqli_free_result($res);
40	mysqli_free_result($res_meta);
41	mysqli_stmt_close($stmt);
42
43	// !mysqli_stmt_prepare($stmt, "SELECT id, label, id + 1 as _id,  concat(label, '_') _label FROM test as _test ORDER BY id ASC LIMIT 3") ||
44	if (!($stmt = mysqli_stmt_init($link)) ||
45		!mysqli_stmt_prepare($stmt, "SELECT id , label, id + 1 AS _id, label AS _label, null AS _null, CONCAT(label, '_') _label_concat  FROM test _test ORDER BY id ASC LIMIT 3") ||
46		!mysqli_stmt_execute($stmt))
47		printf("[006] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
48
49	if (!is_object($res = mysqli_stmt_get_result($stmt)) || 'mysqli_result' != get_class($res)) {
50		printf("[007] Expecting object/mysqli_result got %s/%s, [%d] %s\n",
51			gettype($res), $res, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
52	}
53
54	if (!is_object($res_meta = mysqli_stmt_result_metadata($stmt)) ||
55		'mysqli_result' != get_class($res_meta)) {
56		printf("[008] Expecting object/mysqli_result got %s/%s, [%d] %s\n",
57			gettype($res), $res, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
58	}
59
60	if (($tmp1 = mysqli_num_fields($res)) !== ($tmp2 = mysqli_num_fields($res_meta))) {
61		printf("[009] %s/%s !== %s/%s\n", gettype($tmp1), $tmp1, gettype($tmp2), $tmp2);
62	}
63
64	/*
65	if (($tmp1 = mysqli_field_count($link)) !== ($tmp2 = $res->field_count()))
66		printf("[010] %s/%s !== %s/%s\n", gettype($tmp1), $tmp1, gettype($tmp2), $tmp2);
67
68	if (($tmp1 = $res_meta->field_count()) !== $tmp2)
69		printf("[011] %s/%s !== %s/%s\n", gettype($tmp1), $tmp1, gettype($tmp2), $tmp2);
70	*/
71
72	if (($tmp1 = mysqli_field_tell($res)) !== ($tmp2 = $res_meta->current_field))
73		printf("[012] %s/%s !== %s/%s\n", gettype($tmp1), $tmp1, gettype($tmp2), $tmp2);
74
75	if (0 !== $tmp1)
76		printf("[013] Expecting int/0 got %s/%s\n", gettype($tmp1), $tmp1);
77
78	$fields = array();
79	while ($info = $res->fetch_field())
80		$fields['res'][] = $info;
81	var_dump($info);
82	while ($info = $res_meta->fetch_field())
83		$fields['meta'][] = $info;
84	var_dump($info);
85	$fields['all_res'] = $res->fetch_fields();
86	$fields['all_meta'] = $res_meta->fetch_fields();
87
88	if (count($fields['res']) != count($fields['meta'])) {
89		printf("[014] stmt_get_result indicates %d fields, stmt_result_metadata indicates %d fields\n",
90			count($fields['res']),
91			count($fields['meta']));
92	}
93
94	foreach ($fields['res'] as $k => $info) {
95		printf("%s\n", $info->name);
96		if ($info->name !== $fields['meta'][$k]->name)
97			printf("[015 - %d] Expecting name %s/%s got %s/%s\n",
98				$k, gettype($info->name), $info->name, gettype($fields['meta'][$k]->name), $fields['meta'][$k]->name);
99
100		if ($info->orgname !== $fields['meta'][$k]->orgname)
101			printf("[016 - %d] Expecting orgname %s/%s got %s/%s\n",
102				$k, gettype($info->orgname), $info->orgname, gettype($fields['meta'][$k]->orgname), $fields['meta'][$k]->orgname);
103
104		if ($info->table !== $fields['meta'][$k]->table)
105			printf("[017 - %d] Expecting table %s/%s got %s/%s\n",
106				$k, gettype($info->table), $info->table, gettype($fields['meta'][$k]->table), $fields['meta'][$k]->table);
107
108		if ($info->orgtable !== $fields['meta'][$k]->orgtable)
109			printf("[018 - %d] Expecting orgtable %s/%s got %s/%s\n",
110				$k, gettype($info->orgtable), $info->orgtable, gettype($fields['meta'][$k]->orgtable), $fields['meta'][$k]->orgtable);
111
112		if ($info->def !== $fields['meta'][$k]->def)
113			printf("[019 - %d] Expecting def %s/%s got %s/%s\n",
114				$k, gettype($info->def), $info->def, gettype($fields['meta'][$k]->def), $fields['meta'][$k]->def);
115/*
116		if ($info->max_length !== $fields['meta'][$k]->max_length)
117			printf("[020 - %d] Expecting max_length %s/%s got %s/%s\n",
118				$k, gettype($info->max_length), $info->max_length, gettype($fields['meta'][$k]->max_length), $fields['meta'][$k]->max_length);
119*/
120		if ($info->length !== $fields['meta'][$k]->length)
121			printf("[021 - %d] Expecting length %s/%s got %s/%s\n",
122				$k, gettype($info->length), $info->length, gettype($fields['meta'][$k]->length), $fields['meta'][$k]->length);
123
124		if ($info->charsetnr !== $fields['meta'][$k]->charsetnr)
125			printf("[022 - %d] Expecting charsetnr %s/%s got %s/%s\n",
126				$k, gettype($info->charsetnr), $info->charsetnr, gettype($fields['meta'][$k]->charsetnr), $fields['meta'][$k]->charsetnr);
127
128		if ($info->flags !== $fields['meta'][$k]->flags)
129			printf("[023 - %d] Expecting flags %s/%s got %s/%s\n",
130				$k, gettype($info->flags), $info->flags, gettype($fields['meta'][$k]->flags), $fields['meta'][$k]->flags);
131
132		if ($info->type !== $fields['meta'][$k]->type)
133			printf("[024 - %d] Expecting type %s/%s got %s/%s\n",
134				$k, gettype($info->type), $info->type, gettype($fields['meta'][$k]->type), $fields['meta'][$k]->type);
135
136		if ($info->decimals !== $fields['meta'][$k]->decimals)
137			printf("[025 - %d] Expecting decimals %s/%s got %s/%s\n",
138				$k, getdecimals($info->decimals), $info->decimals, getdecimals($fields['meta'][$k]->decimals), $fields['meta'][$k]->decimals);
139
140		/* Make them equal for the check */
141		$tmp = $fields['all_res'][$k]->max_length;
142		$fields['all_res'][$k]->max_length = $fields['all_meta'][$k]->max_length;
143
144		if ($fields['all_res'][$k] != $fields['all_meta'][$k]) {
145			printf("[026 - %d] fetch_fields() seems to have returned different data, dumping\n", $k);
146			var_dump($fields['all_res'][$k]);
147			var_dump($fields['all_meta'][$k]);
148		}
149		$fields['all_res'][$k]->max_length = $tmp;
150	}
151
152	$num = count($fields['res']);
153	for ($i = 0; $i < 100; $i++) {
154		$pos = mt_rand(-1, $num + 1);
155		if ($pos >= 0 && $pos < $num) {
156			if (true !== mysqli_field_seek($res, $pos))
157				printf("[027] field_seek(res) failed\n");
158			if (true !== $res_meta->field_seek($pos))
159				printf("[028] field_seek(res__meta) failed\n");
160
161			$tmp1 = $res->fetch_field();
162			$tmp2 = mysqli_fetch_field($res_meta);
163			$tmp2->max_length = $tmp1->max_length;
164			if ($tmp1 != $tmp2) {
165				printf("[029] Field info differs, dumping data\n");
166				var_dump($tmp1);
167				var_dump($tmp2);
168			}
169
170			if ($tmp1 != $fields['res'][$pos]) {
171				printf("[030] Field info differs, dumping data\n");
172				var_dump($tmp1);
173				var_dump($fields['res'][$pos]);
174			}
175
176			$pos++;
177			if ($pos !== ($tmp = mysqli_field_tell($res)))
178				printf("[031] Expecting %s/%s got %s/%s\n",
179					gettype($pos), $pos, gettype($tmp), $tmp);
180
181			if ($pos !== ($tmp = mysqli_field_tell($res_meta)))
182				printf("[032] Expecting %s/%s got %s/%s\n",
183					gettype($pos), $pos, gettype($tmp), $tmp);
184		} else {
185
186			if (false !== @mysqli_field_seek($res, $pos))
187				printf("[033] field_seek(%d) did not fail\n", $pos);
188			if (false !== @mysqli_field_seek($res_meta, $pos))
189				printf("[034] field_seek(%d) did not fail\n", $pos);
190		}
191	}
192
193	$res->free_result();
194	mysqli_free_result($res_meta);
195
196	var_dump(mysqli_fetch_field($res));
197
198	mysqli_stmt_close($stmt);
199
200	var_dump(mysqli_fetch_field($res));
201
202	mysqli_close($link);
203
204	var_dump(mysqli_fetch_field($res));
205
206	print "done!";
207?>
208--CLEAN--
209<?php
210	require_once("clean_table.inc");
211?>
212--EXPECTF--
213array(2) {
214  ["id"]=>
215  int(1)
216  ["label"]=>
217  string(1) "a"
218}
219NULL
220bool(false)
221bool(false)
222id
223label
224_id
225_label
226_null
227_label_concat
228
229Warning: mysqli_fetch_field(): Couldn't fetch mysqli_result in %s on line %d
230bool(false)
231
232Warning: mysqli_fetch_field(): Couldn't fetch mysqli_result in %s on line %d
233bool(false)
234
235Warning: mysqli_fetch_field(): Couldn't fetch mysqli_result in %s on line %d
236bool(false)
237done!
238