mysqli_stmt_attr_set.phpt 8.7 KB

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