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