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