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
70    // Make sure that client, connection and result charsets are all the
71    // same. Not sure whether this is strictly necessary.
72    if (!mysqli_set_charset($link, 'utf8'))
73        printf("[%d] %s\n", mysqli_errno($link), mysqli_errno($link));
74
75    $charsetInfo = mysqli_get_charset($link);
76
77    if (!$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1", MYSQLI_USE_RESULT)) {
78        printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
79    }
80
81    var_dump(mysqli_field_seek($res, -1));
82    var_dump(mysqli_fetch_field($res));
83    var_dump(mysqli_field_seek($res, 0));
84    var_dump(mysqli_fetch_field($res));
85    var_dump(mysqli_field_seek($res, 1));
86
87    $field = mysqli_fetch_field($res);
88    var_dump($field);
89    /* label column, result set charset */
90    if ($field->charsetnr != $charsetInfo->number) {
91        printf("[004] Expecting charset %s/%d got %d\n",
92            $charsetInfo->charset, $charsetInfo->number, $field->charsetnr);
93    }
94    if ($field->length != $charsetInfo->max_length) {
95        printf("[005] Expecting length %d got %d\n",
96            $charsetInfo->max_length, $field->max_length);
97    }
98
99    var_dump(mysqli_field_tell($res));
100    var_dump(mysqli_field_seek($res, 2));
101    var_dump(mysqli_fetch_field($res));
102    var_dump(mysqli_field_seek($res, PHP_INT_MAX + 1));
103
104    if (!is_null($tmp = @mysqli_field_seek($res, 0, "too many")))
105        printf("[004] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
106
107    mysqli_free_result($res);
108
109    if (!$res = mysqli_query($link, "SELECT NULL as _null", MYSQLI_STORE_RESULT)) {
110        printf("[005] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
111    }
112    var_dump(mysqli_field_seek($res, 0));
113    var_dump(mysqli_fetch_field($res));
114
115    mysqli_free_result($res);
116
117    var_dump(mysqli_field_seek($res, 0));
118
119    mysqli_close($link);
120    print "done!";
121?>
122--CLEAN--
123<?php
124    require_once("clean_table.inc");
125?>
126--EXPECTF--
127Warning: mysqli_field_seek(): Invalid field offset in %s on line %d
128bool(false)
129object(stdClass)#%d (13) {
130  ["name"]=>
131  string(2) "id"
132  ["orgname"]=>
133  string(2) "id"
134  ["table"]=>
135  string(4) "test"
136  ["orgtable"]=>
137  string(4) "test"
138  ["def"]=>
139  string(0) ""
140  ["db"]=>
141  string(%d) "%s"
142  ["catalog"]=>
143  string(%d) "%s"
144  ["max_length"]=>
145  int(0)
146  ["length"]=>
147  int(11)
148  ["charsetnr"]=>
149  int(63)
150  ["flags"]=>
151  int(49155)
152  ["type"]=>
153  int(3)
154  ["decimals"]=>
155  int(0)
156}
157bool(true)
158object(stdClass)#%d (13) {
159  ["name"]=>
160  string(2) "id"
161  ["orgname"]=>
162  string(2) "id"
163  ["table"]=>
164  string(4) "test"
165  ["orgtable"]=>
166  string(4) "test"
167  ["def"]=>
168  string(0) ""
169  ["db"]=>
170  string(%d) "%s"
171  ["catalog"]=>
172  string(%d) "%s"
173  ["max_length"]=>
174  int(0)
175  ["length"]=>
176  int(11)
177  ["charsetnr"]=>
178  int(63)
179  ["flags"]=>
180  int(49155)
181  ["type"]=>
182  int(3)
183  ["decimals"]=>
184  int(0)
185}
186bool(true)
187object(stdClass)#%d (13) {
188  ["name"]=>
189  string(5) "label"
190  ["orgname"]=>
191  string(5) "label"
192  ["table"]=>
193  string(4) "test"
194  ["orgtable"]=>
195  string(4) "test"
196  ["def"]=>
197  string(0) ""
198  ["db"]=>
199  string(%d) "%s"
200  ["catalog"]=>
201  string(%d) "%s"
202  ["max_length"]=>
203  int(%d)
204  ["length"]=>
205  int(%d)
206  ["charsetnr"]=>
207  int(%d)
208  ["flags"]=>
209  int(0)
210  ["type"]=>
211  int(254)
212  ["decimals"]=>
213  int(0)
214}
215int(2)
216
217Warning: mysqli_field_seek(): Invalid field offset in %s on line %d
218bool(false)
219bool(false)
220
221Warning: mysqli_field_seek() expects parameter 2 to be int, float given in %s on line %d
222NULL
223bool(true)
224object(stdClass)#%d (13) {
225  ["name"]=>
226  string(5) "_null"
227  ["orgname"]=>
228  string(0) ""
229  ["table"]=>
230  string(0) ""
231  ["orgtable"]=>
232  string(0) ""
233  ["def"]=>
234  string(0) ""
235  ["db"]=>
236  string(0) ""
237  ["catalog"]=>
238  string(%d) "%s"
239  ["max_length"]=>
240  int(0)
241  ["length"]=>
242  int(0)
243  ["charsetnr"]=>
244  int(63)
245  ["flags"]=>
246  int(32896)
247  ["type"]=>
248  int(6)
249  ["decimals"]=>
250  int(0)
251}
252
253Warning: mysqli_field_seek(): Couldn't fetch mysqli_result in %s on line %d
254bool(false)
255done!
256