mysqli_stmt_bind_result.phpt 15 KB

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