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

https://gitlab.com/iranjith4/hhvm · PHP · 246 lines · 172 code · 32 blank · 42 comment · 40 complexity · de323554d42e216422599f10bdb04585 MD5 · raw file

  1. <?php
  2. require_once("connect.inc");
  3. $tmp = NULL;
  4. $link = NULL;
  5. $test_table_name = 'test_mysqli_stmt_attr_set_table_1'; require('table.inc');
  6. $valid_attr = array(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH);
  7. if ((mysqli_get_client_version() > 50003) || $IS_MYSQLND) {
  8. $valid_attr[] = MYSQLI_STMT_ATTR_CURSOR_TYPE;
  9. $valid_attr[] = MYSQLI_CURSOR_TYPE_NO_CURSOR;
  10. $valid_attr[] = MYSQLI_CURSOR_TYPE_READ_ONLY;
  11. $valid_attr[] = MYSQLI_CURSOR_TYPE_FOR_UPDATE;
  12. $valid_attr[] = MYSQLI_CURSOR_TYPE_SCROLLABLE;
  13. }
  14. if ((mysqli_get_client_version() > 50007) || $IS_MYSQLND)
  15. $valid_attr[] = MYSQLI_STMT_ATTR_PREFETCH_ROWS;
  16. $stmt = mysqli_stmt_init($link);
  17. $stmt->prepare("SELECT * FROM test_mysqli_stmt_attr_set_table_1");
  18. mt_srand(microtime(true));
  19. for ($i = -100; $i < 1000; $i++) {
  20. if (in_array($i, $valid_attr))
  21. continue;
  22. $invalid_attr = $i;
  23. if (false !== ($tmp = @mysqli_stmt_attr_set($stmt, $invalid_attr, 0))) {
  24. printf("[006a] Expecting boolean/false for attribute %d, got %s/%s\n", $invalid_attr, gettype($tmp), $tmp);
  25. }
  26. }
  27. for ($i = 0; $i < 2; $i++) {
  28. do {
  29. $invalid_attr = mt_rand(-1 * (min(4294967296, PHP_INT_MAX) + 1), min(4294967296, PHP_INT_MAX));
  30. } while (in_array($invalid_attr, $valid_attr));
  31. if (false !== ($tmp = @mysqli_stmt_attr_set($stmt, $invalid_attr, 0))) {
  32. printf("[006b] Expecting boolean/false for attribute %d, got %s/%s\n", $invalid_attr, gettype($tmp), $tmp);
  33. }
  34. }
  35. $stmt->close();
  36. //
  37. // MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH
  38. //
  39. // expecting max_length not to be set and be 0 in all cases
  40. $stmt = mysqli_stmt_init($link);
  41. $stmt->prepare("SELECT label FROM test_mysqli_stmt_attr_set_table_1");
  42. $stmt->execute();
  43. $stmt->store_result();
  44. $res = $stmt->result_metadata();
  45. $fields = $res->fetch_fields();
  46. $max_lengths = array();
  47. foreach ($fields as $k => $meta) {
  48. $max_lengths[$meta->name] = $meta->max_length;
  49. if ($meta->max_length !== 0)
  50. printf("[007] max_length should be not set (= 0), got %s for field %s\n", $meta->max_length, $meta->name);
  51. }
  52. $res->close();
  53. $stmt->close();
  54. // expecting max_length to _be_ set
  55. $stmt = mysqli_stmt_init($link);
  56. $stmt->prepare("SELECT label FROM test_mysqli_stmt_attr_set_table_1");
  57. $stmt->attr_set(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH, 1);
  58. $res = $stmt->attr_get(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH);
  59. if ($res !== 1)
  60. printf("[007.1] max_length should be 1, got %s\n", $res);
  61. $stmt->execute();
  62. $stmt->store_result();
  63. $res = $stmt->result_metadata();
  64. $fields = $res->fetch_fields();
  65. $max_lengths = array();
  66. foreach ($fields as $k => $meta) {
  67. $max_lengths[$meta->name] = $meta->max_length;
  68. if ($meta->max_length === 0)
  69. printf("[008] max_length should be set (!= 0), got %s for field %s\n", $meta->max_length, $meta->name);
  70. }
  71. $res->close();
  72. $stmt->close();
  73. // expecting max_length not to be set
  74. $stmt = mysqli_stmt_init($link);
  75. $stmt->prepare("SELECT label FROM test_mysqli_stmt_attr_set_table_1");
  76. $stmt->attr_set(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH, 0);
  77. $res = $stmt->attr_get(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH);
  78. if ($res !== 0)
  79. printf("[008.1] max_length should be 0, got %s\n", $res);
  80. $stmt->execute();
  81. $stmt->store_result();
  82. $res = $stmt->result_metadata();
  83. $fields = $res->fetch_fields();
  84. $max_lengths = array();
  85. foreach ($fields as $k => $meta) {
  86. $max_lengths[$meta->name] = $meta->max_length;
  87. if ($meta->max_length !== 0)
  88. printf("[009] max_length should not be set (= 0), got %s for field %s\n", $meta->max_length, $meta->name);
  89. }
  90. $res->close();
  91. $stmt->close();
  92. //
  93. // Cursors
  94. //
  95. if (mysqli_get_client_version() > 50003) {
  96. $cursor_types = array(
  97. MYSQLI_CURSOR_TYPE_NO_CURSOR,
  98. MYSQLI_CURSOR_TYPE_READ_ONLY,
  99. MYSQLI_CURSOR_TYPE_FOR_UPDATE,
  100. MYSQLI_CURSOR_TYPE_SCROLLABLE
  101. );
  102. do {
  103. $invalid_cursor_type = mt_rand(-1000, 1000);
  104. } while (in_array($invalid_cursor_type, $cursor_types));
  105. $stmt = mysqli_stmt_init($link);
  106. $stmt->prepare("SELECT id, label FROM test_mysqli_stmt_attr_set_table_1");
  107. if (false !== ($tmp = $stmt->attr_set(MYSQLI_STMT_ATTR_CURSOR_TYPE, MYSQLI_CURSOR_TYPE_FOR_UPDATE)))
  108. printf("[011] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
  109. if (false !== ($tmp = $stmt->attr_set(MYSQLI_STMT_ATTR_CURSOR_TYPE, MYSQLI_CURSOR_TYPE_SCROLLABLE)))
  110. printf("[012] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
  111. if (true !== ($tmp = $stmt->attr_set(MYSQLI_STMT_ATTR_CURSOR_TYPE, MYSQLI_CURSOR_TYPE_NO_CURSOR)))
  112. printf("[013] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
  113. if (true !== ($tmp = $stmt->attr_set(MYSQLI_STMT_ATTR_CURSOR_TYPE, MYSQLI_CURSOR_TYPE_READ_ONLY)))
  114. printf("[014] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
  115. $stmt->close();
  116. $stmt = mysqli_stmt_init($link);
  117. $stmt->prepare("SELECT id, label FROM test_mysqli_stmt_attr_set_table_1");
  118. $stmt->execute();
  119. $id = $label = NULL;
  120. $stmt->bind_result($id, $label);
  121. $results = array();
  122. while ($stmt->fetch())
  123. $results[$id] = $label;
  124. $stmt->close();
  125. if (empty($results))
  126. printf("[015] Results should not be empty, subsequent tests will probably fail!\n");
  127. $stmt = mysqli_stmt_init($link);
  128. $stmt->prepare("SELECT id, label FROM test_mysqli_stmt_attr_set_table_1");
  129. if (true !== ($tmp = $stmt->attr_set(MYSQLI_STMT_ATTR_CURSOR_TYPE, MYSQLI_CURSOR_TYPE_NO_CURSOR)))
  130. printf("[016] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
  131. $stmt->execute();
  132. $id = $label = NULL;
  133. $stmt->bind_result($id, $label);
  134. $results2 = array();
  135. while ($stmt->fetch())
  136. $results2[$id] = $label;
  137. $stmt->close();
  138. if ($results != $results2) {
  139. printf("[017] Results should not differ. Dumping both result sets.\n");
  140. var_dump($results);
  141. var_dump($results2);
  142. }
  143. $stmt = mysqli_stmt_init($link);
  144. $stmt->prepare("SELECT id, label FROM test_mysqli_stmt_attr_set_table_1");
  145. if (true !== ($tmp = $stmt->attr_set(MYSQLI_STMT_ATTR_CURSOR_TYPE, MYSQLI_CURSOR_TYPE_READ_ONLY)))
  146. printf("[018] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
  147. $stmt->execute();
  148. $id = $label = NULL;
  149. $stmt->bind_result($id, $label);
  150. $results2 = array();
  151. while ($stmt->fetch())
  152. $results2[$id] = $label;
  153. $stmt->close();
  154. if ($results != $results2) {
  155. printf("[019] Results should not differ. Dumping both result sets.\n");
  156. var_dump($results);
  157. var_dump($results2);
  158. }
  159. }
  160. //
  161. // MYSQLI_STMT_ATTR_PREFETCH_ROWS
  162. //
  163. if (mysqli_get_client_version() > 50007) {
  164. $stmt = mysqli_stmt_init($link);
  165. $stmt->prepare("SELECT id, label FROM test_mysqli_stmt_attr_set_table_1");
  166. if (true !== ($tmp = $stmt->attr_set(MYSQLI_STMT_ATTR_PREFETCH_ROWS, 1)))
  167. printf("[020] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
  168. $stmt->execute();
  169. $id = $label = NULL;
  170. $stmt->bind_result($id, $label);
  171. $results = array();
  172. while ($stmt->fetch())
  173. $results[$id] = $label;
  174. $stmt->close();
  175. if (empty($results))
  176. printf("[021] Results should not be empty, subsequent tests will probably fail!\n");
  177. /* prefetch is not supported
  178. $stmt = mysqli_stmt_init($link);
  179. $stmt->prepare("SELECT label FROM test_mysqli_stmt_attr_set_table_1");
  180. if (false !== ($tmp = $stmt->attr_set(MYSQLI_STMT_ATTR_PREFETCH_ROWS, -1)))
  181. printf("[022] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
  182. $stmt->close();
  183. $stmt = mysqli_stmt_init($link);
  184. $stmt->prepare("SELECT label FROM test_mysqli_stmt_attr_set_table_1");
  185. if (true !== ($tmp = $stmt->attr_set(MYSQLI_STMT_ATTR_PREFETCH_ROWS, PHP_INT_MAX)))
  186. printf("[023] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
  187. $stmt->close();
  188. $stmt = mysqli_stmt_init($link);
  189. $stmt->prepare("SELECT id, label FROM test_mysqli_stmt_attr_set_table_1");
  190. if (true !== ($tmp = $stmt->attr_set(MYSQLI_STMT_ATTR_PREFETCH_ROWS, 2)))
  191. printf("[024] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
  192. $stmt->execute();
  193. $id = $label = NULL;
  194. $stmt->bind_result($id, $label);
  195. $results2 = array();
  196. while ($stmt->fetch())
  197. $results2[$id] = $label;
  198. $stmt->close();
  199. if ($results != $results2) {
  200. printf("[025] Results should not differ. Dumping both result sets.\n");
  201. var_dump($results);
  202. var_dump($results2);
  203. }
  204. */
  205. }
  206. mysqli_close($link);
  207. print "done!";
  208. ?>
  209. <?php error_reporting(0); ?>
  210. <?php
  211. $test_table_name = 'test_mysqli_stmt_attr_set_table_1'; require_once("clean_table.inc");
  212. ?>