1--TEST--
2mysqli_field_seek()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7?>
8--FILE--
9<?php
10    function mysqli_field_seek_flags($flags) {
11
12        $ret = '';
13
14        if ($flags & MYSQLI_NOT_NULL_FLAG)
15            $ret .= 'MYSQLI_NOT_NULL_FLAG ';
16
17        if ($flags & MYSQLI_PRI_KEY_FLAG)
18            $ret .= 'MYSQLI_PRI_KEY_FLAG ';
19
20        if ($flags & MYSQLI_UNIQUE_KEY_FLAG)
21            $ret .= 'MYSQLI_UNIQUE_KEY_FLAG ';
22
23        if ($flags & MYSQLI_MULTIPLE_KEY_FLAG)
24            $ret .= 'MYSQLI_MULTIPLE_KEY_FLAG ';
25
26        if ($flags & MYSQLI_BLOB_FLAG)
27            $ret .= 'MYSQLI_BLOB_FLAG ';
28
29        if ($flags & MYSQLI_UNSIGNED_FLAG)
30            $ret .= 'MYSQLI_UNSIGNED_FLAG ';
31
32        if ($flags & MYSQLI_ZEROFILL_FLAG)
33            $ret .= 'MYSQLI_ZEROFILL_FLAG ';
34
35        if ($flags & MYSQLI_AUTO_INCREMENT_FLAG)
36            $ret .= 'MYSQLI_AUTO_INCREMENT_FLAG ';
37
38        if ($flags & MYSQLI_TIMESTAMP_FLAG)
39            $ret .= 'MYSQLI_TIMESTAMP_FLAG ';
40
41        if ($flags & MYSQLI_SET_FLAG)
42            $ret .= 'MYSQLI_SET_FLAG ';
43
44        if ($flags & MYSQLI_NUM_FLAG)
45            $ret .= 'MYSQLI_NUM_FLAG ';
46
47        if ($flags & MYSQLI_PART_KEY_FLAG)
48            $ret .= 'MYSQLI_PART_KEY_FLAG ';
49
50        if ($flags & MYSQLI_GROUP_FLAG)
51            $ret .= 'MYSQLI_GROUP_FLAG ';
52
53        return $ret;
54    }
55
56    require_once("connect.inc");
57
58    require('table.inc');
59
60    // Make sure that client, connection and result charsets are all the
61    // same. Not sure whether this is strictly necessary.
62    if (!mysqli_set_charset($link, 'utf8'))
63        printf("[%d] %s\n", mysqli_errno($link), mysqli_errno($link));
64
65    $charsetInfo = mysqli_get_charset($link);
66
67    if (!$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1", MYSQLI_USE_RESULT)) {
68        printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
69    }
70
71    try {
72        var_dump(mysqli_field_seek($res, -1));
73    } catch (\ValueError $e) {
74        echo $e->getMessage() . \PHP_EOL;
75    }
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 != $charsetInfo->number) {
85        printf("[004] Expecting charset %s/%d got %d\n",
86            $charsetInfo->charset, $charsetInfo->number, $field->charsetnr);
87    }
88    if ($field->length != $charsetInfo->max_length) {
89        printf("[005] Expecting length %d got %d\n",
90            $charsetInfo->max_length, $field->max_length);
91    }
92
93    var_dump(mysqli_field_tell($res));
94    try {
95        var_dump(mysqli_field_seek($res, 2));
96    } catch (\ValueError $e) {
97        echo $e->getMessage() . \PHP_EOL;
98    }
99    var_dump(mysqli_fetch_field($res));
100
101    mysqli_free_result($res);
102
103    if (!$res = mysqli_query($link, "SELECT NULL as _null", MYSQLI_STORE_RESULT)) {
104        printf("[005] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
105    }
106    var_dump(mysqli_field_seek($res, 0));
107    var_dump(mysqli_fetch_field($res));
108
109    mysqli_free_result($res);
110
111    try {
112        mysqli_field_seek($res, 0);
113    } catch (Error $exception) {
114        echo $exception->getMessage() . "\n";
115    }
116
117    mysqli_close($link);
118    print "done!";
119?>
120--CLEAN--
121<?php
122    require_once("clean_table.inc");
123?>
124--EXPECTF--
125mysqli_field_seek(): Argument #2 ($index) must be greater than or equal to 0
126object(stdClass)#%d (13) {
127  ["name"]=>
128  string(2) "id"
129  ["orgname"]=>
130  string(2) "id"
131  ["table"]=>
132  string(4) "test"
133  ["orgtable"]=>
134  string(4) "test"
135  ["def"]=>
136  string(0) ""
137  ["db"]=>
138  string(%d) "%s"
139  ["catalog"]=>
140  string(%d) "%s"
141  ["max_length"]=>
142  int(0)
143  ["length"]=>
144  int(11)
145  ["charsetnr"]=>
146  int(63)
147  ["flags"]=>
148  int(49155)
149  ["type"]=>
150  int(3)
151  ["decimals"]=>
152  int(0)
153}
154bool(true)
155object(stdClass)#%d (13) {
156  ["name"]=>
157  string(2) "id"
158  ["orgname"]=>
159  string(2) "id"
160  ["table"]=>
161  string(4) "test"
162  ["orgtable"]=>
163  string(4) "test"
164  ["def"]=>
165  string(0) ""
166  ["db"]=>
167  string(%d) "%s"
168  ["catalog"]=>
169  string(%d) "%s"
170  ["max_length"]=>
171  int(0)
172  ["length"]=>
173  int(11)
174  ["charsetnr"]=>
175  int(63)
176  ["flags"]=>
177  int(49155)
178  ["type"]=>
179  int(3)
180  ["decimals"]=>
181  int(0)
182}
183bool(true)
184object(stdClass)#%d (13) {
185  ["name"]=>
186  string(5) "label"
187  ["orgname"]=>
188  string(5) "label"
189  ["table"]=>
190  string(4) "test"
191  ["orgtable"]=>
192  string(4) "test"
193  ["def"]=>
194  string(0) ""
195  ["db"]=>
196  string(%d) "%s"
197  ["catalog"]=>
198  string(%d) "%s"
199  ["max_length"]=>
200  int(%d)
201  ["length"]=>
202  int(%d)
203  ["charsetnr"]=>
204  int(%d)
205  ["flags"]=>
206  int(0)
207  ["type"]=>
208  int(254)
209  ["decimals"]=>
210  int(0)
211}
212int(2)
213mysqli_field_seek(): Argument #2 ($index) must be less than the number of fields for this result set
214bool(false)
215bool(true)
216object(stdClass)#%d (13) {
217  ["name"]=>
218  string(5) "_null"
219  ["orgname"]=>
220  string(0) ""
221  ["table"]=>
222  string(0) ""
223  ["orgtable"]=>
224  string(0) ""
225  ["def"]=>
226  string(0) ""
227  ["db"]=>
228  string(0) ""
229  ["catalog"]=>
230  string(%d) "%s"
231  ["max_length"]=>
232  int(0)
233  ["length"]=>
234  int(0)
235  ["charsetnr"]=>
236  int(63)
237  ["flags"]=>
238  int(32896)
239  ["type"]=>
240  int(6)
241  ["decimals"]=>
242  int(0)
243}
244mysqli_result object is already closed
245done!
246