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