1--TEST-- 2mysql_fetch_array() 3--SKIPIF-- 4<?php 5require_once('skipif.inc'); 6require_once('skipifconnectfailure.inc'); 7?> 8--FILE-- 9<?php 10include_once "connect.inc"; 11 12$tmp = NULL; 13$link = NULL; 14 15if (NULL !== ($tmp = @mysql_fetch_array())) 16 printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); 17 18if (NULL != ($tmp = @mysql_fetch_array($link))) 19 printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); 20 21require('table.inc'); 22if (!$res = mysql_query("SELECT * FROM test ORDER BY id LIMIT 5", $link)) { 23 printf("[004] [%d] %s\n", mysql_errno($link), mysql_error($link)); 24} 25 26print "[005]\n"; 27var_dump(mysql_fetch_array($res)); 28 29print "[006]\n"; 30var_dump(mysql_fetch_array($res, MYSQL_NUM)); 31 32print "[007]\n"; 33var_dump(mysql_fetch_array($res, MYSQL_BOTH)); 34 35print "[008]\n"; 36var_dump(mysql_fetch_array($res, MYSQL_ASSOC)); 37 38print "[009]\n"; 39var_dump(mysql_fetch_array($res)); 40 41mysql_free_result($res); 42 43if (!$res = mysql_query("SELECT 1 AS a, 2 AS a, 3 AS c, 4 AS C, NULL AS d, true AS e", $link)) { 44 printf("[010] Cannot run query, [%d] %s\n", mysql_errno($link), mysql_error($link)); 45} 46print "[011]\n"; 47var_dump(mysql_fetch_array($res, MYSQL_BOTH)); 48 49mysql_free_result($res); 50if (!$res = mysql_query("SELECT 1 AS a, 2 AS b, 3 AS c, 4 AS C", $link)) { 51 printf("[012] Cannot run query, [%d] %s\n", 52 mysql_errno($link), $mysql_error($link)); 53 exit(1); 54} 55 56do { 57 $illegal_mode = mt_rand(0, 10000); 58} while (in_array($illegal_mode, array(MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH))); 59$tmp = mysql_fetch_array($res, $illegal_mode); 60if (!is_array($tmp)) 61 printf("[013] Expecting array, got %s/%s. [%d] %s\n", 62 gettype($tmp), $tmp, mysql_errno($link), mysql_error($link)); 63 64$tmp = @mysql_fetch_array($res, $illegal_mode); 65if (false !== $tmp) 66 printf("[014] Expecting boolean/false, got %s/%s. [%d] %s\n", 67 gettype($tmp), $tmp, mysql_errno($link), mysql_error($link)); 68 69mysql_free_result($res); 70 71function func_mysql_fetch_array($link, $engine, $sql_type, $sql_value, $php_value, $offset, $regexp_comparison = NULL, $binary_type = false) { 72 73 if (!mysql_query("DROP TABLE IF EXISTS test", $link)) { 74 printf("[%04d] [%d] %s\n", $offset, mysql_errno($link), mysql_error($link)); 75 return false; 76 } 77 78 if (!mysql_query($sql = sprintf("CREATE TABLE test(id INT NOT NULL, label %s, PRIMARY KEY(id)) ENGINE = %s", $sql_type, $engine), $link)) { 79 // don't bail, engine might not support the datatype 80 return false; 81 } 82 83 if (is_null($php_value) && !mysql_query($sql = sprintf("INSERT INTO test(id, label) VALUES (1, NULL)"), $link)) { 84 printf("[%04d] [%d] %s\n", $offset + 1, mysql_errno($link), mysql_error($link)); 85 return false; 86 } 87 88 if (!is_null($php_value)) { 89 if (is_int($sql_value) && !mysql_query(sprintf("INSERT INTO test(id, label) VALUES (1, '%d')", $sql_value), $link)) { 90 printf("[%04d] [%d] %s\n", $offset + 1, mysql_errno($link), mysql_error($link)); 91 return false; 92 } else if (!is_int($sql_value) && !mysql_query(sprintf("INSERT INTO test(id, label) VALUES (1, '%s')", $sql_value), $link)) { 93 printf("[%04d] [%d] %s\n", $offset + 1, mysql_errno($link), mysql_error($link)); 94 return false; 95 } 96 } 97 98 if (!$res = mysql_query("SELECT id, label FROM test", $link)) { 99 printf("[%04d] [%d] %s\n", $offset + 2, mysql_errno($link), mysql_error($link)); 100 return false; 101 } 102 103 if (!$row = mysql_fetch_array($res, MYSQL_BOTH)) { 104 printf("[%04d] [%d] %s\n", $offset + 3, mysql_errno($link), mysql_error($link)); 105 return false; 106 } 107 108 if ($regexp_comparison) { 109 if (!preg_match($regexp_comparison, (string)$row['label']) || !preg_match($regexp_comparison, (string)$row[1])) { 110 printf("[%04d] Expecting %s/%s [reg exp = %s], got %s/%s resp. %s/%s. [%d] %s\n", $offset + 4, 111 gettype($php_value), $php_value, $regexp_comparison, 112 gettype($row[1]), $row[1], 113 gettype($row['label']), $row['label'], mysql_errno($link), mysql_error($link)); 114 return false; 115 } 116 } else if ((gettype($php_value) == 'unicode') && $binary_type) { 117 // Unicode is on and we are told that the MySQL column type is a binary type. 118 // Don't expect a unicode value from the database, you'll get binary string 119 if (($row['label'] != $php_value) || ($row[1] != $php_value)) { 120 printf("[%04d] Expecting %s/%s, got %s/%s resp. %s/%s. [%d] %s\n", $offset + 5, 121 gettype($php_value), $php_value, 122 gettype($row[1]), $row[1], 123 gettype($row['label']), $row['label'], mysql_errno($link), mysql_error($link)); 124 return false; 125 } 126 if (gettype($row['label']) == 'unicode') { 127 printf("[%04d] SQL Type: '%s', binary columns are supposed to return binary string and not unicode\n", 128 $offset + 6, $sql_type); 129 return false; 130 } 131 } else { 132 if (($row['label'] !== $php_value) || ($row[1] != $php_value)) { 133 printf("[%04d] Expecting %s/%s, got %s/%s resp. %s/%s. [%d] %s\n", $offset + 7, 134 gettype($php_value), $php_value, 135 gettype($row[1]), $row[1], 136 gettype($row['label']), $row['label'], mysql_errno($link), mysql_error($link)); 137 return false; 138 } 139 } 140 141 return true; 142} 143 144function func_mysql_fetch_array_make_string($len) { 145 146 $ret = ''; 147 for ($i = 0; $i < $len; $i++) 148 $ret .= chr(mt_rand(65, 90)); 149 150 return $ret; 151} 152 153func_mysql_fetch_array($link, $engine, "TINYINT", -11, "-11", 20); 154func_mysql_fetch_array($link, $engine, "TINYINT", NULL, NULL, 30); 155func_mysql_fetch_array($link, $engine, "TINYINT UNSIGNED", 1, "1", 40); 156func_mysql_fetch_array($link, $engine, "TINYINT UNSIGNED", NULL, NULL, 50); 157 158func_mysql_fetch_array($link, $engine, "BOOL", 1, "1", 60); 159func_mysql_fetch_array($link, $engine, "BOOL", NULL, NULL, 70); 160func_mysql_fetch_array($link, $engine, "BOOLEAN", 0, "0", 80); 161func_mysql_fetch_array($link, $engine, "BOOLEAN", NULL, NULL, 90); 162 163func_mysql_fetch_array($link, $engine, "SMALLINT", -32768, "-32768", 100); 164func_mysql_fetch_array($link, $engine, "SMALLINT", 32767, "32767", 110); 165func_mysql_fetch_array($link, $engine, "SMALLINT", NULL, NULL, 120); 166func_mysql_fetch_array($link, $engine, "SMALLINT UNSIGNED", 65535, "65535", 130); 167func_mysql_fetch_array($link, $engine, "SMALLINT UNSIGNED", NULL, NULL, 140); 168 169func_mysql_fetch_array($link, $engine, "MEDIUMINT", -8388608, "-8388608", 150); 170func_mysql_fetch_array($link, $engine, "MEDIUMINT", 8388607, "8388607", 160); 171func_mysql_fetch_array($link, $engine, "MEDIUMINT", NULL, NULL, 170); 172func_mysql_fetch_array($link, $engine, "MEDIUMINT UNSIGNED", 16777215, "16777215", 180); 173func_mysql_fetch_array($link, $engine, "MEDIUMINT UNSIGNED", NULL, NULL, 190); 174 175func_mysql_fetch_array($link, $engine, "INTEGER", -2147483648, "-2147483648", 200); 176func_mysql_fetch_array($link, $engine, "INTEGER", 2147483647, "2147483647", 210); 177func_mysql_fetch_array($link, $engine, "INTEGER", NULL, NULL, 220); 178func_mysql_fetch_array($link, $engine, "INTEGER UNSIGNED", 4294967295, "4294967295", 230); 179func_mysql_fetch_array($link, $engine, "INTEGER UNSIGNED", NULL, NULL, 240); 180 181// func_mysql_fetch_array($link, $engine, "BIGINT", -9223372036854775808, "-9.22337e+018", 250, "/-9\.22337e\+[0]?18/iu"); 182func_mysql_fetch_array($link, $engine, "BIGINT", NULL, NULL, 260); 183// func_mysql_fetch_array($link, $engine, "BIGINT UNSIGNED", 18446744073709551615, "1.84467e+019", 270, "/1\.84467e\+[0]?19/iu"); 184func_mysql_fetch_array($link, $engine, "BIGINT UNSIGNED", NULL, NULL, 280); 185 186func_mysql_fetch_array($link, $engine, "FLOAT", -9223372036854775808 - 1.1, "-9.22337e+18", 290, "/-9\.22337e\+?[0]?18/iu"); 187func_mysql_fetch_array($link, $engine, "FLOAT", NULL, NULL, 300); 188func_mysql_fetch_array($link, $engine, "FLOAT UNSIGNED", 18446744073709551615 + 1.1, "1.84467e+19", 310, "/1\.84467e\+?[0]?19/iu"); 189func_mysql_fetch_array($link, $engine, "FLOAT UNSIGNED ", NULL, NULL, 320); 190 191func_mysql_fetch_array($link, $engine, "DOUBLE(10,2)", -99999999.99, "-99999999.99", 330); 192func_mysql_fetch_array($link, $engine, "DOUBLE(10,2)", NULL, NULL, 340); 193func_mysql_fetch_array($link, $engine, "DOUBLE(10,2) UNSIGNED", 99999999.99, "99999999.99", 350); 194func_mysql_fetch_array($link, $engine, "DOUBLE(10,2) UNSIGNED", NULL, NULL, 360); 195 196func_mysql_fetch_array($link, $engine, "DECIMAL(10,2)", -99999999.99, "-99999999.99", 370); 197func_mysql_fetch_array($link, $engine, "DECIMAL(10,2)", NULL, NULL, 380); 198func_mysql_fetch_array($link, $engine, "DECIMAL(10,2)", 99999999.99, "99999999.99", 390); 199func_mysql_fetch_array($link, $engine, "DECIMAL(10,2)", NULL, NULL, 400); 200 201// don't care about date() strict TZ warnings... 202func_mysql_fetch_array($link, $engine, "DATE", @date('Y-m-d'), @date('Y-m-d'), 410); 203func_mysql_fetch_array($link, $engine, "DATE NOT NULL", @date('Y-m-d'), @date('Y-m-d'), 420); 204func_mysql_fetch_array($link, $engine, "DATE", NULL, NULL, 430); 205 206func_mysql_fetch_array($link, $engine, "DATETIME", @date('Y-m-d H:i:s'), @date('Y-m-d H:i:s'), 440); 207func_mysql_fetch_array($link, $engine, "DATETIME NOT NULL", @date('Y-m-d H:i:s'), @date('Y-m-d H:i:s'), 450); 208func_mysql_fetch_array($link, $engine, "DATETIME", NULL, NULL, 460); 209 210func_mysql_fetch_array($link, $engine, "TIMESTAMP", @date('Y-m-d H:i:s'), @date('Y-m-d H:i:s'), 470); 211 212func_mysql_fetch_array($link, $engine, "TIME", @date('H:i:s'), @date('H:i:s'), 480); 213func_mysql_fetch_array($link, $engine, "TIME NOT NULL", @date('H:i:s'), @date('H:i:s'), 490); 214func_mysql_fetch_array($link, $engine, "TIME", NULL, NULL, 500); 215 216func_mysql_fetch_array($link, $engine, "YEAR", @date('Y'), @date('Y'), 510); 217func_mysql_fetch_array($link, $engine, "YEAR NOT NULL", @date('Y'), @date('Y'), 520); 218func_mysql_fetch_array($link, $engine, "YEAR", NULL, NULL, 530); 219 220$string255 = func_mysql_fetch_array_make_string(255); 221 222func_mysql_fetch_array($link, $engine, "CHAR(1)", "a", "a", 540); 223func_mysql_fetch_array($link, $engine, "CHAR(255)", $string255, $string255, 550); 224func_mysql_fetch_array($link, $engine, "CHAR(1) NOT NULL", "a", "a", 560); 225func_mysql_fetch_array($link, $engine, "CHAR(1)", NULL, NULL, 570); 226 227$string65k = func_mysql_fetch_array_make_string(65400); 228 229func_mysql_fetch_array($link, $engine, "VARCHAR(1)", "a", "a", 580); 230func_mysql_fetch_array($link, $engine, "VARCHAR(255)", $string255, $string255, 590); 231func_mysql_fetch_array($link, $engine, "VARCHAR(65400)", $string65k, $string65k, 600); 232func_mysql_fetch_array($link, $engine, "VARCHAR(1) NOT NULL", "a", "a", 610); 233func_mysql_fetch_array($link, $engine, "VARCHAR(1)", NULL, NULL, 620); 234 235func_mysql_fetch_array($link, $engine, "BINARY(1)", "a", "a", 630, null , true); 236func_mysql_fetch_array($link, $engine, "BINARY(1) NOT NULL", "b", "b", 650, null , true); 237func_mysql_fetch_array($link, $engine, "BINARY(1)", NULL, NULL, 660, null , true); 238 239func_mysql_fetch_array($link, $engine, "VARBINARY(1)", "a", "a", 670, null , true); 240func_mysql_fetch_array($link, $engine, "VARBINARY(1) NOT NULL", "b", "b", 690, null , true); 241func_mysql_fetch_array($link, $engine, "VARBINARY(1)", NULL, NULL, 700, null , true); 242 243func_mysql_fetch_array($link, $engine, "TINYBLOB", "a", "a", 710, null , true); 244func_mysql_fetch_array($link, $engine, "TINYBLOB NOT NULL", "b", "b", 730, null , true); 245func_mysql_fetch_array($link, $engine, "TINYBLOB", NULL, NULL, 740, null , true); 246 247func_mysql_fetch_array($link, $engine, "TINYTEXT", "a", "a", 750); 248func_mysql_fetch_array($link, $engine, "TINYTEXT NOT NULL", "a", "a", 760); 249func_mysql_fetch_array($link, $engine, "TINYTEXT", NULL, NULL, 770); 250 251func_mysql_fetch_array($link, $engine, "BLOB", "a", "a", 780, null , true); 252func_mysql_fetch_array($link, $engine, "BLOB", NULL, NULL, 790, null , true); 253 254func_mysql_fetch_array($link, $engine, "TEXT", "a", "a", 800); 255func_mysql_fetch_array($link, $engine, "TEXT", NULL, NULL, 820); 256 257func_mysql_fetch_array($link, $engine, "MEDIUMBLOB", "a", "a", 830, null , true); 258func_mysql_fetch_array($link, $engine, "MEDIUMBLOB", NULL, NULL, 850, null , true); 259 260func_mysql_fetch_array($link, $engine, "MEDIUMTEXT", "a", "a", 860); 261func_mysql_fetch_array($link, $engine, "MEDIUMTEXT", NULL, NULL, 880); 262 263func_mysql_fetch_array($link, $engine, "LONGBLOB", "a", "a", 890, null , true); 264func_mysql_fetch_array($link, $engine, "LONGBLOB", NULL, NULL, 910, null , true); 265 266func_mysql_fetch_array($link, $engine, "ENUM('a', 'b')", "a", "a", 920); 267func_mysql_fetch_array($link, $engine, "ENUM('a', 'b')", NULL, NULL, 930); 268 269func_mysql_fetch_array($link, $engine, "SET('a', 'b')", "a", "a", 940); 270func_mysql_fetch_array($link, $engine, "SET('a', 'b')", NULL, NULL, 950); 271 272mysql_close($link); 273 274if (false !== ($tmp = mysql_fetch_array($res, MYSQL_ASSOC))) 275printf("[015] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); 276 277print "done!"; 278?> 279--CLEAN-- 280<?php 281require_once("clean_table.inc"); 282?> 283--EXPECTF-- 284Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in %s on line %d 285[005] 286array(4) { 287 [0]=> 288 %unicode|string%(1) "1" 289 [%u|b%"id"]=> 290 %unicode|string%(1) "1" 291 [1]=> 292 %unicode|string%(1) "a" 293 [%u|b%"label"]=> 294 %unicode|string%(1) "a" 295} 296[006] 297array(2) { 298 [0]=> 299 %unicode|string%(1) "2" 300 [1]=> 301 %unicode|string%(1) "b" 302} 303[007] 304array(4) { 305 [0]=> 306 %unicode|string%(1) "3" 307 [%u|b%"id"]=> 308 %unicode|string%(1) "3" 309 [1]=> 310 %unicode|string%(1) "c" 311 [%u|b%"label"]=> 312 %unicode|string%(1) "c" 313} 314[008] 315array(2) { 316 [%u|b%"id"]=> 317 %unicode|string%(1) "4" 318 [%u|b%"label"]=> 319 %unicode|string%(1) "d" 320} 321[009] 322array(4) { 323 [0]=> 324 %unicode|string%(1) "5" 325 [%u|b%"id"]=> 326 %unicode|string%(1) "5" 327 [1]=> 328 %unicode|string%(1) "e" 329 [%u|b%"label"]=> 330 %unicode|string%(1) "e" 331} 332[011] 333array(11) { 334 [0]=> 335 %unicode|string%(1) "1" 336 [%u|b%"a"]=> 337 %unicode|string%(1) "2" 338 [1]=> 339 %unicode|string%(1) "2" 340 [2]=> 341 %unicode|string%(1) "3" 342 [%u|b%"c"]=> 343 %unicode|string%(1) "3" 344 [3]=> 345 %unicode|string%(1) "4" 346 [%u|b%"C"]=> 347 %unicode|string%(1) "4" 348 [4]=> 349 NULL 350 [%u|b%"d"]=> 351 NULL 352 [5]=> 353 %unicode|string%(1) "1" 354 [%u|b%"e"]=> 355 %unicode|string%(1) "1" 356} 357 358Warning: mysql_fetch_array(): The result type should be either MYSQL_NUM, MYSQL_ASSOC or MYSQL_BOTH in %s on line %d 359 360Warning: mysql_fetch_array(): %d is not a valid MySQL result resource in %s on line %d 361done! 362