mysqli_fetch_array.phpt 15 KB

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