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