1--TEST--
2mysql_fetch_field()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7?>
8--FILE--
9<?php
10	include "connect.inc";
11
12	$tmp    = NULL;
13	$link   = NULL;
14
15	// Note: no SQL type tests, internally the same function gets used as for mysql_fetch_array() which does a lot of SQL type test
16	if (!is_null($tmp = @mysql_fetch_field()))
17		printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
18
19	if (NULL !== ($tmp = @mysql_fetch_field($link)))
20		printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
21
22	require('table.inc');
23
24	$version = mysql_get_server_info($link);
25	if (!preg_match('@(\d+)\.(\d+)\.(\d+)@ism', $version, $matches))
26		printf("[003] Cannot get server version\n");
27	$version = ($matches[1] * 100) + ($matches[2] * 10) + $matches[3];
28
29	if (!$res = mysql_query("SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 1", $link)) {
30		printf("[004] [%d] %s\n", mysql_errno($link), mysql_error($link));
31	}
32
33	while ($tmp = mysql_fetch_field($res))
34		var_dump($tmp);
35	var_dump($tmp);
36
37	mysql_free_result($res);
38
39	if (!$res = mysql_query("SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 1", $link)) {
40		printf("[005] [%d] %s\n", mysql_errno($link), mysql_error($link));
41	}
42	if (false !== ($tmp = mysql_fetch_field($res, PHP_INT_MAX - 1)))
43		printf("[006] Expecting boolean/false got %s/%s\n", gettype($tmp), var_export($tmp, true));
44
45	mysql_free_result($res);
46
47	if (false !== ($tmp = mysql_fetch_field($res)))
48		printf("[007] Expecting boolean/false, got %s/%s\n", gettype($tmp), var_export($tmp, true));
49
50	$types = array(
51		'BIT'               => array(1, 'int'),
52		'TINYINT'           => array(1, 'int'),
53		'BOOL'              => array('true', 'int'),
54		'BOOL'              => array(1, 'int'),
55		'SMALLINT'          => array(32767, 'int'),
56		'MEDIUMINT'         => array(8388607, 'int'),
57		'INT'               => array(100, 'int'),
58		'BIGINT'            => array(100, 'int'),
59		'FLOAT'             => array(100, 'real'),
60		'DOUBLE'            => array(100, 'real'),
61		'DECIMAL'           => array(100, 'real'),
62		'DATE'              => array(@date('Y-m-d'), 'date'),
63		'DATETIME'          => array(@date('Y-m-d H:i:s'), 'datetime'),
64		'TIMESTAMP'         => array(@date('Y-m-d H:i:s'), 'timestamp'),
65		'TIME'              => array(@date('H:i:s'), 'time'),
66		'YEAR'              => array(@date('Y'), 'year'),
67		'CHAR(1)'           => array('a', 'string'),
68		'VARCHAR(1)'        => array('a', 'string'),
69		'BINARY(1)'         => array('a', 'string'),
70		'VARBINARY(1)'      => array('a', 'string'),
71		'TINYBLOB'          => array('a', 'blob'),
72		'TINYTEXT'          => array('a', 'blob'),
73		'BLOB'              => array('a', 'blob'),
74		'TEXT'              => array('a', 'blob'),
75		'MEDIUMBLOB'        => array('a', 'blob'),
76		'MEDIUMTEXT'        => array('a', 'blob'),
77		'LONGBLOB'          => array('a', 'blob'),
78		'LONGTEXT'          => array('a', 'blob'),
79		'ENUM("a", "b")'    => array('a', 'string'), /* !!! */
80		'SET("a", "b")'     => array('a', 'string'), /* !!! */
81	);
82
83	foreach ($types as $type_name => $type_desc) {
84		if (!mysql_query("DROP TABLE IF EXISTS test", $link))
85			printf("[008/%s] [%d] %s\n", $type_name, mysql_errno($link), mysql_error($link));
86		if (!mysql_query(sprintf("CREATE TABLE test(id INT, label %s) ENGINE = %s", $type_name, $engine), $link)) {
87			// server and/or engine might not support the data type
88			continue;
89		}
90
91		if (is_string($type_desc[0]))
92			$insert = sprintf("INSERT INTO test(id, label) VALUES (1, '%s')", $type_desc[0]);
93		else
94			$insert = sprintf("INSERT INTO test(id, label) VALUES (1, %s)", $type_desc[0]);
95
96		if (!mysql_query($insert, $link)) {
97			if (1366 == mysql_errno($link)) {
98				/* Strict SQL mode - 1366, Incorrect integer value: 'true' for column 'label' at row 1 */
99				continue;
100			}
101			printf("[009/%s] [%d] %s\n", $type_name, mysql_errno($link), mysql_error($link));
102			continue;
103		}
104		if (!$res = mysql_query("SELECT id, label FROM test", $link)) {
105			printf("[010/%s] [%d] %s\n", $type_name, mysql_errno($link), mysql_error($link));
106			continue;
107		}
108		if (!$tmp = mysql_fetch_field($res, 1)) {
109			printf("[011/%s] [%d] %s\n", $type_name, mysql_errno($link), mysql_error($link));
110		}
111
112		if ($type_desc[1] != $tmp->type) {
113			printf("[012/%s] Expecting type '%s' got '%s'\n", $type_name, $type_desc[1], $tmp->type);
114		}
115		mysql_free_result($res);
116	}
117
118	if (!mysql_query("DROP TABLE IF EXISTS test", $link))
119		printf("[013] [%d] %s\n", mysql_errno($link), mysql_error($link));
120
121	if (!mysql_query("CREATE TABLE test(id INT DEFAULT 1)"))
122		printf("[014] [%d] %s\n", mysql_errno($link), mysql_error($link));
123
124	if (!mysql_query("INSERT INTO test(id) VALUES (2)"))
125		printf("[015] [%d] %s\n", mysql_errno($link), mysql_error($link));
126
127	if (!$res = mysql_query("SELECT id FROM test", $link)) {
128		printf("[016] [%d] %s\n", mysql_errno($link), mysql_error($link));
129	}
130
131	var_dump(mysql_fetch_field($res));
132	mysql_free_result($res);
133
134	if (!$res = mysql_query("SELECT id FROM test", $link)) {
135		printf("[017] [%d] %s\n", mysql_errno($link), mysql_error($link));
136	}
137	$res = mysql_list_fields($db, 'test');
138	$found = false;
139	while ($tmp = mysql_fetch_field($res)) {
140		if ($tmp->name == 'id') {
141			printf("Fetch field from mysql_list_fields result set.\n");
142			$found = true;
143			var_dump($tmp);
144		}
145	}
146	if (!$found)
147		printf("[018] mysqli_list_fields result set processing has failed.\n");
148
149	mysql_free_result($res);
150
151	mysql_close($link);
152	print "done!";
153?>
154--CLEAN--
155<?php
156require_once("clean_table.inc");
157?>
158--EXPECTF--
159Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in %s on line %d
160object(stdClass)#%d (13) {
161  [%u|b%"name"]=>
162  %unicode|string%(2) "ID"
163  [%u|b%"table"]=>
164  %unicode|string%(4) "TEST"
165  [%u|b%"def"]=>
166  %unicode|string%(0) ""
167  [%u|b%"max_length"]=>
168  int(1)
169  [%u|b%"not_null"]=>
170  int(1)
171  [%u|b%"primary_key"]=>
172  int(1)
173  [%u|b%"multiple_key"]=>
174  int(0)
175  [%u|b%"unique_key"]=>
176  int(0)
177  [%u|b%"numeric"]=>
178  int(1)
179  [%u|b%"blob"]=>
180  int(0)
181  [%u|b%"type"]=>
182  %unicode|string%(3) "int"
183  [%u|b%"unsigned"]=>
184  int(0)
185  [%u|b%"zerofill"]=>
186  int(0)
187}
188object(stdClass)#%d (13) {
189  [%u|b%"name"]=>
190  %unicode|string%(5) "label"
191  [%u|b%"table"]=>
192  %unicode|string%(4) "TEST"
193  [%u|b%"def"]=>
194  %unicode|string%(0) ""
195  [%u|b%"max_length"]=>
196  int(1)
197  [%u|b%"not_null"]=>
198  int(0)
199  [%u|b%"primary_key"]=>
200  int(0)
201  [%u|b%"multiple_key"]=>
202  int(0)
203  [%u|b%"unique_key"]=>
204  int(0)
205  [%u|b%"numeric"]=>
206  int(0)
207  [%u|b%"blob"]=>
208  int(0)
209  [%u|b%"type"]=>
210  %unicode|string%(6) "string"
211  [%u|b%"unsigned"]=>
212  int(0)
213  [%u|b%"zerofill"]=>
214  int(0)
215}
216bool(false)
217
218Warning: mysql_fetch_field(): Bad field offset in %s on line %d
219
220Warning: mysql_fetch_field(): %d is not a valid MySQL result resource in %s on line %d
221object(stdClass)#%d (13) {
222  [%u|b%"name"]=>
223  %unicode|string%(2) "id"
224  [%u|b%"table"]=>
225  %unicode|string%(4) "test"
226  [%u|b%"def"]=>
227  %unicode|string%(0) ""
228  [%u|b%"max_length"]=>
229  int(1)
230  [%u|b%"not_null"]=>
231  int(0)
232  [%u|b%"primary_key"]=>
233  int(0)
234  [%u|b%"multiple_key"]=>
235  int(0)
236  [%u|b%"unique_key"]=>
237  int(0)
238  [%u|b%"numeric"]=>
239  int(1)
240  [%u|b%"blob"]=>
241  int(0)
242  [%u|b%"type"]=>
243  %unicode|string%(3) "int"
244  [%u|b%"unsigned"]=>
245  int(0)
246  [%u|b%"zerofill"]=>
247  int(0)
248}
249Fetch field from mysql_list_fields result set.
250object(stdClass)#%d (13) {
251  [%u|b%"name"]=>
252  %unicode|string%(2) "id"
253  [%u|b%"table"]=>
254  %unicode|string%(4) "test"
255  [%u|b%"def"]=>
256  %unicode|string%(1) "1"
257  [%u|b%"max_length"]=>
258  int(0)
259  [%u|b%"not_null"]=>
260  int(0)
261  [%u|b%"primary_key"]=>
262  int(0)
263  [%u|b%"multiple_key"]=>
264  int(0)
265  [%u|b%"unique_key"]=>
266  int(0)
267  [%u|b%"numeric"]=>
268  int(1)
269  [%u|b%"blob"]=>
270  int(0)
271  [%u|b%"type"]=>
272  %unicode|string%(3) "int"
273  [%u|b%"unsigned"]=>
274  int(0)
275  [%u|b%"zerofill"]=>
276  int(0)
277}
278done!
279