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-- 159object(stdClass)#%d (13) { 160 [%u|b%"name"]=> 161 %unicode|string%(2) "ID" 162 [%u|b%"table"]=> 163 %unicode|string%(4) "TEST" 164 [%u|b%"def"]=> 165 %unicode|string%(0) "" 166 [%u|b%"max_length"]=> 167 int(1) 168 [%u|b%"not_null"]=> 169 int(1) 170 [%u|b%"primary_key"]=> 171 int(1) 172 [%u|b%"multiple_key"]=> 173 int(0) 174 [%u|b%"unique_key"]=> 175 int(0) 176 [%u|b%"numeric"]=> 177 int(1) 178 [%u|b%"blob"]=> 179 int(0) 180 [%u|b%"type"]=> 181 %unicode|string%(3) "int" 182 [%u|b%"unsigned"]=> 183 int(0) 184 [%u|b%"zerofill"]=> 185 int(0) 186} 187object(stdClass)#%d (13) { 188 [%u|b%"name"]=> 189 %unicode|string%(5) "label" 190 [%u|b%"table"]=> 191 %unicode|string%(4) "TEST" 192 [%u|b%"def"]=> 193 %unicode|string%(0) "" 194 [%u|b%"max_length"]=> 195 int(1) 196 [%u|b%"not_null"]=> 197 int(0) 198 [%u|b%"primary_key"]=> 199 int(0) 200 [%u|b%"multiple_key"]=> 201 int(0) 202 [%u|b%"unique_key"]=> 203 int(0) 204 [%u|b%"numeric"]=> 205 int(0) 206 [%u|b%"blob"]=> 207 int(0) 208 [%u|b%"type"]=> 209 %unicode|string%(6) "string" 210 [%u|b%"unsigned"]=> 211 int(0) 212 [%u|b%"zerofill"]=> 213 int(0) 214} 215bool(false) 216 217Warning: mysql_fetch_field(): Bad field offset in %s on line %d 218 219Warning: mysql_fetch_field(): %d is not a valid MySQL result resource in %s on line %d 220object(stdClass)#%d (13) { 221 [%u|b%"name"]=> 222 %unicode|string%(2) "id" 223 [%u|b%"table"]=> 224 %unicode|string%(4) "test" 225 [%u|b%"def"]=> 226 %unicode|string%(0) "" 227 [%u|b%"max_length"]=> 228 int(1) 229 [%u|b%"not_null"]=> 230 int(0) 231 [%u|b%"primary_key"]=> 232 int(0) 233 [%u|b%"multiple_key"]=> 234 int(0) 235 [%u|b%"unique_key"]=> 236 int(0) 237 [%u|b%"numeric"]=> 238 int(1) 239 [%u|b%"blob"]=> 240 int(0) 241 [%u|b%"type"]=> 242 %unicode|string%(3) "int" 243 [%u|b%"unsigned"]=> 244 int(0) 245 [%u|b%"zerofill"]=> 246 int(0) 247} 248Fetch field from mysql_list_fields result set. 249object(stdClass)#%d (13) { 250 [%u|b%"name"]=> 251 %unicode|string%(2) "id" 252 [%u|b%"table"]=> 253 %unicode|string%(4) "test" 254 [%u|b%"def"]=> 255 %unicode|string%(1) "1" 256 [%u|b%"max_length"]=> 257 int(0) 258 [%u|b%"not_null"]=> 259 int(0) 260 [%u|b%"primary_key"]=> 261 int(0) 262 [%u|b%"multiple_key"]=> 263 int(0) 264 [%u|b%"unique_key"]=> 265 int(0) 266 [%u|b%"numeric"]=> 267 int(1) 268 [%u|b%"blob"]=> 269 int(0) 270 [%u|b%"type"]=> 271 %unicode|string%(3) "int" 272 [%u|b%"unsigned"]=> 273 int(0) 274 [%u|b%"zerofill"]=> 275 int(0) 276} 277done! 278