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