mysql_fetch_array.phpt 14 KB

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