1--TEST--
2mysqli_field_seek()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
7require_once('skipifconnectfailure.inc');
8?>
9--FILE--
10<?php
11	function mysqli_field_seek_flags($flags) {
12
13		$ret = '';
14
15		if ($flags & MYSQLI_NOT_NULL_FLAG)
16			$ret .= 'MYSQLI_NOT_NULL_FLAG ';
17
18		if ($flags & MYSQLI_PRI_KEY_FLAG)
19			$ret .= 'MYSQLI_PRI_KEY_FLAG ';
20
21		if ($flags & MYSQLI_UNIQUE_KEY_FLAG)
22			$ret .= 'MYSQLI_UNIQUE_KEY_FLAG ';
23
24		if ($flags & MYSQLI_MULTIPLE_KEY_FLAG)
25			$ret .= 'MYSQLI_MULTIPLE_KEY_FLAG ';
26
27		if ($flags & MYSQLI_BLOB_FLAG)
28			$ret .= 'MYSQLI_BLOB_FLAG ';
29
30		if ($flags & MYSQLI_UNSIGNED_FLAG)
31			$ret .= 'MYSQLI_UNSIGNED_FLAG ';
32
33		if ($flags & MYSQLI_ZEROFILL_FLAG)
34			$ret .= 'MYSQLI_ZEROFILL_FLAG ';
35
36		if ($flags & MYSQLI_AUTO_INCREMENT_FLAG)
37			$ret .= 'MYSQLI_AUTO_INCREMENT_FLAG ';
38
39		if ($flags & MYSQLI_TIMESTAMP_FLAG)
40			$ret .= 'MYSQLI_TIMESTAMP_FLAG ';
41
42		if ($flags & MYSQLI_SET_FLAG)
43			$ret .= 'MYSQLI_SET_FLAG ';
44
45		if ($flags & MYSQLI_NUM_FLAG)
46			$ret .= 'MYSQLI_NUM_FLAG ';
47
48		if ($flags & MYSQLI_PART_KEY_FLAG)
49			$ret .= 'MYSQLI_PART_KEY_FLAG ';
50
51		if ($flags & MYSQLI_GROUP_FLAG)
52			$ret .= 'MYSQLI_GROUP_FLAG ';
53
54		return $ret;
55	}
56
57	require_once("connect.inc");
58
59	$tmp    = NULL;
60	$link   = NULL;
61
62	if (!is_null($tmp = @mysqli_field_seek()))
63		printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
64
65	if (!is_null($tmp = @mysqli_field_seek($link)))
66		printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
67
68	require('table.inc');
69	$charsets = my_get_charsets($link);
70
71	if (!$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1", MYSQLI_USE_RESULT)) {
72		printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
73	}
74
75	var_dump(mysqli_field_seek($res, -1));
76	var_dump(mysqli_fetch_field($res));
77	var_dump(mysqli_field_seek($res, 0));
78	var_dump(mysqli_fetch_field($res));
79	var_dump(mysqli_field_seek($res, 1));
80
81	$field = mysqli_fetch_field($res);
82	var_dump($field);
83	/* label column, result set charset */
84	if ($field->charsetnr != $charsets['results']['nr']) {
85		printf("[004] Expecting charset %s/%d got %d\n",
86			$charsets['results']['charset'],
87			$charsets['results']['nr'], $field->charsetnr);
88	}
89	if ($field->length != (1 * $charsets['results']['maxlen'])) {
90		printf("[005] Expecting length %d got %d\n",
91			$charsets['results']['maxlen'],
92			$field->max_length);
93	}
94
95	var_dump(mysqli_field_tell($res));
96	var_dump(mysqli_field_seek($res, 2));
97	var_dump(mysqli_fetch_field($res));
98	var_dump(mysqli_field_seek($res, PHP_INT_MAX + 1));
99
100	if (!is_null($tmp = @mysqli_field_seek($res, 0, "too many")))
101		printf("[004] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
102
103	mysqli_free_result($res);
104
105	if (!$res = mysqli_query($link, "SELECT NULL as _null", MYSQLI_STORE_RESULT)) {
106		printf("[005] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
107	}
108	var_dump(mysqli_field_seek($res, 0));
109	var_dump(mysqli_fetch_field($res));
110
111	mysqli_free_result($res);
112
113	var_dump(mysqli_field_seek($res, 0));
114
115	mysqli_close($link);
116	print "done!";
117?>
118--CLEAN--
119<?php
120	require_once("clean_table.inc");
121?>
122--EXPECTF--
123Warning: mysqli_field_seek(): Invalid field offset in %s on line %d
124bool(false)
125object(stdClass)#%d (13) {
126  [%u|b%"name"]=>
127  %unicode|string%(2) "id"
128  [%u|b%"orgname"]=>
129  %unicode|string%(2) "id"
130  [%u|b%"table"]=>
131  %unicode|string%(4) "test"
132  [%u|b%"orgtable"]=>
133  %unicode|string%(4) "test"
134  [%u|b%"def"]=>
135  %unicode|string%(0) ""
136  [%u|b%"db"]=>
137  %unicode|string%(%d) "%s"
138  [%u|b%"catalog"]=>
139  %unicode|string%(%d) "%s"
140  [%u|b%"max_length"]=>
141  int(0)
142  [%u|b%"length"]=>
143  int(11)
144  [%u|b%"charsetnr"]=>
145  int(63)
146  [%u|b%"flags"]=>
147  int(49155)
148  [%u|b%"type"]=>
149  int(3)
150  [%u|b%"decimals"]=>
151  int(0)
152}
153bool(true)
154object(stdClass)#%d (13) {
155  [%u|b%"name"]=>
156  %unicode|string%(2) "id"
157  [%u|b%"orgname"]=>
158  %unicode|string%(2) "id"
159  [%u|b%"table"]=>
160  %unicode|string%(4) "test"
161  [%u|b%"orgtable"]=>
162  %unicode|string%(4) "test"
163  [%u|b%"def"]=>
164  %unicode|string%(0) ""
165  [%u|b%"db"]=>
166  %unicode|string%(%d) "%s"
167  [%u|b%"catalog"]=>
168  %unicode|string%(%d) "%s"
169  [%u|b%"max_length"]=>
170  int(0)
171  [%u|b%"length"]=>
172  int(11)
173  [%u|b%"charsetnr"]=>
174  int(63)
175  [%u|b%"flags"]=>
176  int(49155)
177  [%u|b%"type"]=>
178  int(3)
179  [%u|b%"decimals"]=>
180  int(0)
181}
182bool(true)
183object(stdClass)#%d (13) {
184  [%u|b%"name"]=>
185  %unicode|string%(5) "label"
186  [%u|b%"orgname"]=>
187  %unicode|string%(5) "label"
188  [%u|b%"table"]=>
189  %unicode|string%(4) "test"
190  [%u|b%"orgtable"]=>
191  %unicode|string%(4) "test"
192  [%u|b%"def"]=>
193  %unicode|string%(0) ""
194  [%u|b%"db"]=>
195  %unicode|string%(%d) "%s"
196  [%u|b%"catalog"]=>
197  %unicode|string%(%d) "%s"
198  [%u|b%"max_length"]=>
199  int(%d)
200  [%u|b%"length"]=>
201  int(%d)
202  [%u|b%"charsetnr"]=>
203  int(%d)
204  [%u|b%"flags"]=>
205  int(0)
206  [%u|b%"type"]=>
207  int(254)
208  [%u|b%"decimals"]=>
209  int(0)
210}
211int(2)
212
213Warning: mysqli_field_seek(): Invalid field offset in %s on line %d
214bool(false)
215bool(false)
216
217Warning: mysqli_field_seek(): Invalid field offset in %s on line %d
218bool(false)
219bool(true)
220object(stdClass)#3 (13) {
221  [%u|b%"name"]=>
222  %unicode|string%(5) "_null"
223  [%u|b%"orgname"]=>
224  %unicode|string%(0) ""
225  [%u|b%"table"]=>
226  %unicode|string%(0) ""
227  [%u|b%"orgtable"]=>
228  %unicode|string%(0) ""
229  [%u|b%"def"]=>
230  %unicode|string%(0) ""
231  [%u|b%"db"]=>
232  %unicode|string%(0) ""
233  [%u|b%"catalog"]=>
234  %unicode|string%(%d) "%s"
235  [%u|b%"max_length"]=>
236  int(0)
237  [%u|b%"length"]=>
238  int(0)
239  [%u|b%"charsetnr"]=>
240  int(63)
241  [%u|b%"flags"]=>
242  int(32896)
243  [%u|b%"type"]=>
244  int(6)
245  [%u|b%"decimals"]=>
246  int(0)
247}
248
249Warning: mysqli_field_seek(): Couldn't fetch mysqli_result in %s on line %d
250NULL
251done!