PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/test/zend/bad/ext/mysql/tests/mysql_fetch_array.php

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