1--TEST--
2mysqli_fetch_field()
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once('skipifconnectfailure.inc');
8?>
9--FILE--
10<?php
11    require_once("connect.inc");
12
13    // Note: no SQL type tests, internally the same function gets used as for mysqli_fetch_array() which does a lot of SQL type test
14
15    require('table.inc');
16
17    // Make sure that client, connection and result charsets are all the
18    // same. Not sure whether this is strictly necessary.
19    if (!mysqli_set_charset($link, 'utf8'))
20        printf("[%d] %s\n", mysqli_errno($link), mysqli_errno($link));
21
22    $charsetInfo = mysqli_get_charset($link);
23
24    if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 1")) {
25        printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
26    }
27
28    /* ID column, binary charset */
29    $tmp = mysqli_fetch_field($res);
30    var_dump($tmp);
31
32    /* label column, result set charset */
33    $tmp = mysqli_fetch_field($res);
34    var_dump($tmp);
35    if ($tmp->charsetnr != $charsetInfo->number) {
36        printf("[004] Expecting charset %s/%d got %d\n",
37            $charsetInfo->charset, $charsetInfo->number, $tmp->charsetnr);
38    }
39    if ($tmp->db != $db) {
40        printf("011] Expecting database '%s' got '%s'\n",
41            $db, $tmp->db);
42    }
43
44    var_dump(mysqli_fetch_field($res));
45
46    mysqli_free_result($res);
47
48    // Read http://bugs.php.net/bug.php?id=42344 on defaults!
49    try {
50        mysqli_fetch_field($res);
51    } catch (Error $exception) {
52        echo $exception->getMessage() . "\n";
53    }
54
55    if (!mysqli_query($link, "DROP TABLE IF EXISTS test"))
56        printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
57
58    if (!mysqli_query($link, "CREATE TABLE test(id INT NOT NULL DEFAULT 1)"))
59        printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
60
61    if (!mysqli_query($link, "INSERT INTO test(id) VALUES (2)"))
62        printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
63
64    if (!$res = mysqli_query($link, "SELECT id as _default_test FROM test")) {
65        printf("[010] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
66    }
67    var_dump(mysqli_fetch_assoc($res));
68    /* binary */
69    var_dump(mysqli_fetch_field($res));
70    mysqli_free_result($res);
71
72    mysqli_close($link);
73
74    print "done!";
75?>
76--CLEAN--
77<?php
78    require_once("clean_table.inc");
79?>
80--EXPECTF--
81object(stdClass)#%d (13) {
82  ["name"]=>
83  string(2) "ID"
84  ["orgname"]=>
85  string(2) "id"
86  ["table"]=>
87  string(4) "TEST"
88  ["orgtable"]=>
89  string(%d) "%s"
90  ["def"]=>
91  string(0) ""
92  ["db"]=>
93  string(%d) "%s"
94  ["catalog"]=>
95  string(%d) "%s"
96  ["max_length"]=>
97  int(0)
98  ["length"]=>
99  int(11)
100  ["charsetnr"]=>
101  int(63)
102  ["flags"]=>
103  int(49155)
104  ["type"]=>
105  int(3)
106  ["decimals"]=>
107  int(0)
108}
109object(stdClass)#%d (13) {
110  ["name"]=>
111  string(5) "label"
112  ["orgname"]=>
113  string(5) "label"
114  ["table"]=>
115  string(4) "TEST"
116  ["orgtable"]=>
117  string(%d) "%s"
118  ["def"]=>
119  string(0) ""
120  ["db"]=>
121  string(%d) "%s"
122  ["catalog"]=>
123  string(%d) "%s"
124  ["max_length"]=>
125  int(%d)
126  ["length"]=>
127  int(%d)
128  ["charsetnr"]=>
129  int(%d)
130  ["flags"]=>
131  int(0)
132  ["type"]=>
133  int(254)
134  ["decimals"]=>
135  int(0)
136}
137bool(false)
138mysqli_result object is already closed
139array(1) {
140  ["_default_test"]=>
141  string(1) "2"
142}
143object(stdClass)#%d (13) {
144  ["name"]=>
145  string(13) "_default_test"
146  ["orgname"]=>
147  string(2) "id"
148  ["table"]=>
149  string(%d) "%s"
150  ["orgtable"]=>
151  string(%d) "%s"
152  ["def"]=>
153  string(0) ""
154  ["db"]=>
155  string(%d) "%s"
156  ["catalog"]=>
157  string(%d) "%s"
158  ["max_length"]=>
159  int(0)
160  ["length"]=>
161  int(11)
162  ["charsetnr"]=>
163  int(63)
164  ["flags"]=>
165  int(32769)
166  ["type"]=>
167  int(3)
168  ["decimals"]=>
169  int(0)
170}
171done!
172