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