PageRenderTime 25ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/hphp/test/zend/bad/ext/mysqli/tests/mysqli_stmt_bind_result.php

https://gitlab.com/iranjith4/hhvm
PHP | 298 lines | 226 code | 58 blank | 14 comment | 34 complexity | 75bc89dbdb58d87b668501cb81bebba9 MD5 | raw file
  1. <?php
  2. require_once("connect.inc");
  3. $hint_str_or_unicode = (version_compare(PHP_VERSION, '5.9.9', '>') == 1) ? "unicode":"string";
  4. $tmp = NULL;
  5. $link = NULL;
  6. $test_table_name = 'test_mysqli_stmt_bind_result_table_1'; require('table.inc');
  7. $stmt = mysqli_stmt_init($link);
  8. if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test_mysqli_stmt_bind_result_table_1 ORDER BY id LIMIT 1"))
  9. printf("[002a] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
  10. mysqli_stmt_close($stmt);
  11. $stmt = mysqli_stmt_init($link);
  12. $id = null;
  13. $label = null;
  14. $foo = null;
  15. if (!is_null($tmp = mysqli_stmt_bind_result($stmt)))
  16. printf("[003] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  17. if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test_mysqli_stmt_bind_result_table_1 ORDER BY id LIMIT 1"))
  18. printf("[004] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
  19. if (false !== ($tmp = mysqli_stmt_bind_result($stmt, $id)))
  20. printf("[005] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
  21. if (true !== ($tmp = mysqli_stmt_bind_result($stmt, $id, $label)))
  22. printf("[006] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
  23. if (false !== ($tmp = mysqli_stmt_bind_result($stmt, $id, $label, $foo)))
  24. printf("[007] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
  25. if (!mysqli_stmt_execute($stmt))
  26. printf("[008] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
  27. while (mysqli_stmt_fetch($stmt)) {
  28. var_dump($id);
  29. var_dump($label);
  30. }
  31. mysqli_stmt_close($stmt);
  32. function func_mysqli_stmt_bind_result($link, $engine, $bind_type, $sql_type, $bind_value, $offset, $type_hint = null) {
  33. if (!mysqli_query($link, "DROP TABLE IF EXISTS test_mysqli_stmt_bind_result_table_1")) {
  34. printf("[%04d] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
  35. return false;
  36. }
  37. if (!mysqli_query($link, sprintf("CREATE TABLE test_mysqli_stmt_bind_result_table_1(id INT, label %s, PRIMARY KEY(id)) ENGINE = %s", $sql_type, $engine))) {
  38. // don't bail - column type might not be supported by the server, ignore this
  39. return false;
  40. }
  41. if (!$stmt = mysqli_stmt_init($link)) {
  42. printf("[%04d] [%d] %s\n", $offset + 1, mysqli_errno($link), mysqli_error($link));
  43. return false;
  44. }
  45. if (!mysqli_stmt_prepare($stmt, "INSERT INTO test_mysqli_stmt_bind_result_table_1(id, label) VALUES (?, ?)")) {
  46. printf("[%04d] [%d] %s\n", $offset + 2, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
  47. return false;
  48. }
  49. $id = null;
  50. if (!mysqli_stmt_bind_param($stmt, "i" . $bind_type, $id, $bind_value)) {
  51. printf("[%04d] [%d] %s\n", $offset + 3, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
  52. mysqli_stmt_close($stmt);
  53. return false;
  54. }
  55. for ($id = 1; $id < 4; $id++) {
  56. if (!mysqli_stmt_execute($stmt)) {
  57. printf("[%04d] [%d] %s\n", $offset + 3 + $id, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
  58. mysqli_stmt_close($stmt);
  59. return false;
  60. }
  61. }
  62. mysqli_stmt_close($stmt);
  63. $stmt = mysqli_stmt_init($link);
  64. if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test_mysqli_stmt_bind_result_table_1")) {
  65. printf("[%04d] [%d] %s\n", $offset + 7, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
  66. mysqli_stmt_close($stmt);
  67. return false;
  68. }
  69. if (!mysqli_stmt_execute($stmt)) {
  70. printf("[%04d] [%d] %s\n", $offset + 8, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
  71. mysqli_stmt_close($stmt);
  72. return false;
  73. }
  74. $result = mysqli_stmt_result_metadata($stmt);
  75. $bind_res = null;
  76. if (!mysqli_stmt_bind_result($stmt, $id, $bind_res)) {
  77. printf("[%04d] [%d] %s\n", $offset + 9, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
  78. mysqli_stmt_close($stmt);
  79. return false;
  80. }
  81. $num = 0;
  82. $fields = mysqli_fetch_fields($result);
  83. while (mysqli_stmt_fetch($stmt)) {
  84. if (!gettype($bind_res)=="unicode") {
  85. if ($bind_res !== $bind_value && (!$type_hint || ($type_hint !== gettype($bind_res)))) {
  86. printf("[%04d] [%d] Expecting %s/'%s' [type hint = %s], got %s/'%s'\n",
  87. $offset + 10, $num,
  88. gettype($bind_value), $bind_value, $type_hint,
  89. gettype($bind_res), $bind_res);
  90. mysqli_stmt_close($stmt);
  91. return false;
  92. }
  93. }
  94. $num++;
  95. }
  96. if ($num != 3) {
  97. printf("[%04d] [%d] %s, expecting 3 results, got only %d results\n",
  98. $offset + 11, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt), $num);
  99. mysqli_stmt_close($stmt);
  100. return false;
  101. }
  102. mysqli_stmt_close($stmt);
  103. return true;
  104. }
  105. function func_mysqli_stmt_bind_make_string($len) {
  106. $ret = '';
  107. for ($i = 0; $i < $len; $i++)
  108. $ret .= chr(mt_rand(65, 90));
  109. return $ret;
  110. }
  111. func_mysqli_stmt_bind_result($link, $engine, "i", "TINYINT", -11, 20);
  112. func_mysqli_stmt_bind_result($link, $engine, "i", "TINYINT", NULL, 40);
  113. func_mysqli_stmt_bind_result($link, $engine, "i", "TINYINT UNSIGNED", 1, 60);
  114. func_mysqli_stmt_bind_result($link, $engine, "i", "TINYINT UNSIGNED", NULL, 80);
  115. func_mysqli_stmt_bind_result($link, $engine, "i", "BOOL", 1, 100);
  116. func_mysqli_stmt_bind_result($link, $engine, "i", "BOOL", NULL, 120);
  117. func_mysqli_stmt_bind_result($link, $engine, "i", "BOOLEAN", 0, 140);
  118. func_mysqli_stmt_bind_result($link, $engine, "i", "BOOLEAN", NULL, 160);
  119. func_mysqli_stmt_bind_result($link, $engine, "i", "SMALLINT", -32768, 180);
  120. func_mysqli_stmt_bind_result($link, $engine, "i", "SMALLINT", 32767, 200);
  121. func_mysqli_stmt_bind_result($link, $engine, "i", "SMALLINT", NULL, 220);
  122. func_mysqli_stmt_bind_result($link, $engine, "i", "SMALLINT UNSIGNED", 65535, 240);
  123. func_mysqli_stmt_bind_result($link, $engine, "i", "SMALLINT UNSIGNED", NULL, 260);
  124. func_mysqli_stmt_bind_result($link, $engine, "d", "MEDIUMINT", -8388608, 280, "integer");
  125. func_mysqli_stmt_bind_result($link, $engine, "d", "MEDIUMINT", 8388607, 300, "integer");
  126. func_mysqli_stmt_bind_result($link, $engine, "d", "MEDIUMINT", NULL, 320);
  127. func_mysqli_stmt_bind_result($link, $engine, "d", "MEDIUMINT UNSIGNED", 16777215, 340, "integer");
  128. func_mysqli_stmt_bind_result($link, $engine, "d", "MEDIUMINT UNSIGNED", NULL, 360);
  129. func_mysqli_stmt_bind_result($link, $engine, "i", "INTEGER", (defined("PHP_INT_MAX")) ? max(-1 * PHP_INT_MAX + 1, -2147483648) : 1, 380);
  130. func_mysqli_stmt_bind_result($link, $engine, "i", "INTEGER", -2147483647, 400, "integer");
  131. func_mysqli_stmt_bind_result($link, $engine, "i", "INTEGER", (defined("PHP_INT_MAX")) ? min(2147483647, PHP_INT_MAX) : 1, 420);
  132. func_mysqli_stmt_bind_result($link, $engine, "i", "INTEGER", NULL, 440);
  133. func_mysqli_stmt_bind_result($link, $engine, "i", "INTEGER UNSIGNED", (defined("PHP_INT_MAX")) ? min(4294967295, 2147483647) : 1, 460);
  134. func_mysqli_stmt_bind_result($link, $engine, "i", "INTEGER UNSIGNED", 4294967295, 480, (defined("PHP_INT_MAX") && (4294967295 > PHP_INT_MAX)) ? "string" : null);
  135. func_mysqli_stmt_bind_result($link, $engine, "i", "INTEGER UNSIGNED", NULL, 500);
  136. /* test is broken too: we bind "integer" but value is a float
  137. func_mysqli_stmt_bind_result($link, $engine, "i", "BIGINT", -9223372036854775808, 520);
  138. func_mysqli_stmt_bind_result($link, $engine, "i", "BIGINT UNSIGNED", 18446744073709551615, 560);
  139. */
  140. func_mysqli_stmt_bind_result($link, $engine, "i", "BIGINT", NULL, 540);
  141. func_mysqli_stmt_bind_result($link, $engine, "i", "BIGINT UNSIGNED", NULL, 580);
  142. func_mysqli_stmt_bind_result($link, $engine, "i", "BIGINT", -1, 1780);
  143. func_mysqli_stmt_bind_result($link, $engine, "i", "BIGINT UNSIGNED", 1, 1800);
  144. func_mysqli_stmt_bind_result($link, $engine, "i", "BIGINT", -1 * PHP_INT_MAX + 1, 1820);
  145. func_mysqli_stmt_bind_result($link, $engine, "i", "BIGINT UNSIGNED", PHP_INT_MAX, 1840);
  146. func_mysqli_stmt_bind_result($link, $engine, "s", "BIGINT UNSIGNED", "18446744073709551615", 1860);
  147. func_mysqli_stmt_bind_result($link, $engine, "s", "BIGINT", "-9223372036854775808", 1880);
  148. func_mysqli_stmt_bind_result($link, $engine, "d", "FLOAT", -9223372036854775808 - 1.1, 600);
  149. func_mysqli_stmt_bind_result($link, $engine, "d", "FLOAT", NULL, 620);
  150. func_mysqli_stmt_bind_result($link, $engine, "d", "FLOAT UNSIGNED", 18446744073709551615 + 1.1, 640);
  151. func_mysqli_stmt_bind_result($link, $engine, "d", "FLOAT UNSIGNED ", NULL, 660);
  152. // Yes, we need the temporary variable. The PHP casting will fouls us otherwise.
  153. $tmp = strval('-99999999.99');
  154. func_mysqli_stmt_bind_result($link, $engine, "d", "DOUBLE(10,2)", $tmp, 680, "string");
  155. func_mysqli_stmt_bind_result($link, $engine, "d", "DOUBLE(10,2)", NULL, 700);
  156. $tmp = strval('99999999.99');
  157. func_mysqli_stmt_bind_result($link, $engine, "d", "DOUBLE(10,2) UNSIGNED", $tmp , 720, "string");
  158. func_mysqli_stmt_bind_result($link, $engine, "d", "DOUBLE(10,2) UNSIGNED", NULL, 740);
  159. $tmp = strval('-99999999.99');
  160. func_mysqli_stmt_bind_result($link, $engine, "d", "DECIMAL(10,2)", $tmp, 760, "string");
  161. func_mysqli_stmt_bind_result($link, $engine, "d", "DECIMAL(10,2)", NULL, 780);
  162. $tmp = strval('99999999.99');
  163. func_mysqli_stmt_bind_result($link, $engine, "d", "DECIMAL(10,2)", $tmp, 800, "string");
  164. func_mysqli_stmt_bind_result($link, $engine, "d", "DECIMAL(10,2)", NULL, 820);
  165. // don't care about date() strict TZ warnings...
  166. func_mysqli_stmt_bind_result($link, $engine, "s", "DATE", @date('Y-m-d'), 840);
  167. func_mysqli_stmt_bind_result($link, $engine, "s", "DATE NOT NULL", @date('Y-m-d'), 860);
  168. func_mysqli_stmt_bind_result($link, $engine, "s", "DATE", NULL, 880);
  169. func_mysqli_stmt_bind_result($link, $engine, "s", "DATETIME", @date('Y-m-d H:i:s'), 900);
  170. func_mysqli_stmt_bind_result($link, $engine, "s", "DATETIME NOT NULL", @date('Y-m-d H:i:s'), 920);
  171. func_mysqli_stmt_bind_result($link, $engine, "s", "DATETIME", NULL, 940);
  172. func_mysqli_stmt_bind_result($link, $engine, "s", "TIMESTAMP", @date('Y-m-d H:i:s'), 960);
  173. func_mysqli_stmt_bind_result($link, $engine, "s", "TIME", @date('H:i:s'), 980);
  174. func_mysqli_stmt_bind_result($link, $engine, "s", "TIME NOT NULL", @date('H:i:s'), 1000);
  175. func_mysqli_stmt_bind_result($link, $engine, "s", "TIME", NULL, 1020);
  176. $tmp = intval(@date('Y'));
  177. func_mysqli_stmt_bind_result($link, $engine, "s", "YEAR", $tmp, 1040, "integer");
  178. func_mysqli_stmt_bind_result($link, $engine, "s", "YEAR NOT NULL", $tmp, 1060, "integer");
  179. func_mysqli_stmt_bind_result($link, $engine, "s", "YEAR", NULL, 1080);
  180. $string255 = func_mysqli_stmt_bind_make_string(255);
  181. func_mysqli_stmt_bind_result($link, $engine, "s", "CHAR(1)", "a", 1110, $hint_str_or_unicode);
  182. func_mysqli_stmt_bind_result($link, $engine, "s", "CHAR(255)", $string255, 1120, $hint_str_or_unicode);
  183. func_mysqli_stmt_bind_result($link, $engine, "s", "CHAR(1) NOT NULL", "a", 1140, $hint_str_or_unicode);
  184. func_mysqli_stmt_bind_result($link, $engine, "s", "CHAR(1)", NULL, 1160);
  185. $string65k = func_mysqli_stmt_bind_make_string(65535);
  186. func_mysqli_stmt_bind_result($link, $engine, "s", "VARCHAR(1)", "a", 1180, $hint_str_or_unicode);
  187. func_mysqli_stmt_bind_result($link, $engine, "s", "VARCHAR(255)", $string255, 1200, $hint_str_or_unicode);
  188. func_mysqli_stmt_bind_result($link, $engine, "s", "VARCHAR(65635)", $string65k, 1220, $hint_str_or_unicode);
  189. func_mysqli_stmt_bind_result($link, $engine, "s", "VARCHAR(1) NOT NULL", "a", 1240, $hint_str_or_unicode);
  190. func_mysqli_stmt_bind_result($link, $engine, "s", "VARCHAR(1)", NULL, 1260);
  191. func_mysqli_stmt_bind_result($link, $engine, "s", "BINARY(1)", "a", 1280);
  192. func_mysqli_stmt_bind_result($link, $engine, "s", "BINARY(1)", chr(0), 1300);
  193. func_mysqli_stmt_bind_result($link, $engine, "s", "BINARY(1) NOT NULL", "b", 1320);
  194. func_mysqli_stmt_bind_result($link, $engine, "s", "BINARY(1)", NULL, 1340);
  195. func_mysqli_stmt_bind_result($link, $engine, "s", "VARBINARY(1)", "a", 1360);
  196. func_mysqli_stmt_bind_result($link, $engine, "s", "VARBINARY(1)", chr(0), 1380);
  197. func_mysqli_stmt_bind_result($link, $engine, "s", "VARBINARY(1) NOT NULL", "b", 1400);
  198. func_mysqli_stmt_bind_result($link, $engine, "s", "VARBINARY(1)", NULL, 1420);
  199. func_mysqli_stmt_bind_result($link, $engine, "s", "TINYBLOB", "a", 1440);
  200. func_mysqli_stmt_bind_result($link, $engine, "s", "TINYBLOB", chr(0), 1460);
  201. func_mysqli_stmt_bind_result($link, $engine, "s", "TINYBLOB NOT NULL", "b", 1480);
  202. func_mysqli_stmt_bind_result($link, $engine, "s", "TINYBLOB", NULL, 1500);
  203. func_mysqli_stmt_bind_result($link, $engine, "s", "TINYTEXT", "a", 1520, $hint_str_or_unicode);
  204. func_mysqli_stmt_bind_result($link, $engine, "s", "TINYTEXT NOT NULL", "a", 1540, $hint_str_or_unicode);
  205. func_mysqli_stmt_bind_result($link, $engine, "s", "TINYTEXT", NULL, 1560, $hint_str_or_unicode);
  206. // Note: you cannot insert any blob values this way. But you can check the API at least partly this way
  207. // Extra BLOB tests are in mysqli_stmt_send_long()
  208. func_mysqli_stmt_bind_result($link, $engine, "b", "BLOB", b"", 1580);
  209. func_mysqli_stmt_bind_result($link, $engine, "b", "TEXT", "", 1600, $hint_str_or_unicode);
  210. func_mysqli_stmt_bind_result($link, $engine, "b", "MEDIUMBLOB", b"", 1620);
  211. func_mysqli_stmt_bind_result($link, $engine, "b", "MEDIUMTEXT", "", 1640, $hint_str_or_unicode);
  212. /* Is this one related? http://bugs.php.net/bug.php?id=35759 */
  213. if (($IS_MYSQLND) || (!$IS_MYSQLND && (ini_get('memory_limit') > 4294967296))) {
  214. /* NOTE: the MySQL Client Library - not mysqlnd - will allocate
  215. a hugge max_length(type) = 4GB bind buffer */
  216. func_mysqli_stmt_bind_result($link, $engine, "b", "LONGBLOB", "", 1660);
  217. func_mysqli_stmt_bind_result($link, $engine, "b", "LONGTEXT", "", 1680, $hint_str_or_unicode);
  218. }
  219. func_mysqli_stmt_bind_result($link, $engine, "s", "ENUM('a', 'b')", "a", 1700, $hint_str_or_unicode);
  220. func_mysqli_stmt_bind_result($link, $engine, "s", "ENUM('a', 'b')", NULL, 1720, $hint_str_or_unicode);
  221. func_mysqli_stmt_bind_result($link, $engine, "s", "SET('a', 'b')", "a", 1740, $hint_str_or_unicode);
  222. func_mysqli_stmt_bind_result($link, $engine, "s", "SET('a', 'b')", NULL, 1760, $hint_str_or_unicode);
  223. if (mysqli_get_server_version($link) >= 50600)
  224. func_mysqli_stmt_bind_result($link, $engine, "s", "TIME", "13:31:34.123456", 1770, "13:31:34");
  225. /* Check that the function alias exists. It's a deprecated function,
  226. but we have not announce the removal so far, therefore we need to check for it */
  227. $stmt = mysqli_stmt_init($link);
  228. if (!mysqli_stmt_prepare($stmt, "INSERT INTO test_mysqli_stmt_bind_result_table_1(id, label) VALUES (1000, 'z')"))
  229. printf("[3001] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
  230. $id = null;
  231. if (false !== @mysqli_stmt_bind_result($stmt, $id))
  232. printf("[3002] Bind result should not be allowed");
  233. mysqli_stmt_close($stmt);
  234. mysqli_close($link);
  235. print "done!";
  236. ?>
  237. <?php error_reporting(0); ?>
  238. <?php
  239. $test_table_name = 'test_mysqli_stmt_bind_result_table_1'; require_once("clean_table.inc");
  240. ?>