PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/facebook/hiphop-php
PHP | 244 lines | 189 code | 38 blank | 17 comment | 71 complexity | 53c3d95e288563c482acffb9014de346 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. require_once("connect.inc");
  3. $test_table_name = 'test_mysqli_ps_select_union_table_1'; require_once("table.inc");
  4. // Regular (non-prepared) queries
  5. print "Using CAST('somestring' AS CHAR)...\n";
  6. if (!($res = $link->query("SELECT CAST('one' AS CHAR) AS column1 UNION SELECT CAST('three' AS CHAR) UNION SELECT CAST('two' AS CHAR)")))
  7. printf("[001] [%d] %s\n", $link->errno, $link->error);
  8. $data = array();
  9. while ($row = $res->fetch_assoc()) {
  10. $data[] = $row['column1'];
  11. var_dump($row['column1']);
  12. }
  13. $res->free();
  14. // Prepared Statements
  15. if (!($stmt = $link->prepare("SELECT CAST('one' AS CHAR) AS column1 UNION SELECT CAST('three' AS CHAR) UNION SELECT CAST('two' AS CHAR)")))
  16. printf("[002] [%d] %s\n", $link->errno, $link->error);
  17. $column1 = null;
  18. if (!$stmt->execute() || !$stmt->bind_result($column1))
  19. printf("[003] [%d] %s\n", $stmt->errno, $stmt->error);
  20. $index = 0;
  21. while ($stmt->fetch()) {
  22. if ($data[$index] != $column1) {
  23. printf("[004] Row %d, expecting %s/%s got %s/%s\n",
  24. $index + 1, gettype($data[$index]), $data[$index], gettype($column1), $column1);
  25. }
  26. $index++;
  27. }
  28. $stmt->close();
  29. if ($IS_MYSQLND) {
  30. /*
  31. Advantage mysqlnd -
  32. The metadata mysqlnd has availabe after prepare is better than
  33. the one made availabe by the MySQL Client Library (libmysql).
  34. "libmysql" will give wrong results and that is OK -
  35. http://bugs.mysql.com/bug.php?id=47483
  36. */
  37. if (!($stmt = $link->prepare("SELECT CAST('one' AS CHAR) AS column1 UNION SELECT CAST('three' AS CHAR) UNION SELECT CAST('two' AS CHAR)")))
  38. printf("[005] [%d] %s\n", $link->errno, $link->error);
  39. $column1 = null;
  40. /* Note: bind_result before execute */
  41. if (!$stmt->bind_result($column1) || !$stmt->execute())
  42. printf("[006] [%d] %s\n", $stmt->errno, $stmt->error);
  43. $index = 0;
  44. while ($stmt->fetch()) {
  45. if ($data[$index] != $column1) {
  46. printf("[007] Row %d, expecting %s/%s got %s/%s\n",
  47. $index + 1, gettype($data[$index]), $data[$index], gettype($column1), $column1);
  48. }
  49. $index++;
  50. }
  51. $stmt->close();
  52. }
  53. // Regular (non-prepared) queries
  54. print "Mixing CAST('somestring'AS CHAR), integer and CAST(integer AS CHAR)...\n";
  55. if (!($res = $link->query("SELECT 1 AS column1 UNION SELECT CAST('three' AS CHAR) UNION SELECT CAST(2 AS CHAR)")))
  56. printf("[008] [%d] %s\n", $link->errno, $link->error);
  57. $data = array();
  58. while ($row = $res->fetch_assoc()) {
  59. $data[] = $row['column1'];
  60. }
  61. $res->free();
  62. // Prepared Statements
  63. if (!($stmt = $link->prepare("SELECT 1 AS column1 UNION SELECT CAST('three' AS CHAR) UNION SELECT CAST(2 AS CHAR)")))
  64. printf("[009] [%d] %s\n", $link->errno, $link->error);
  65. $column1 = null;
  66. if (!$stmt->execute() || !$stmt->bind_result($column1))
  67. printf("[010] [%d] %s\n", $stmt->errno, $stmt->error);
  68. $index = 0;
  69. while ($stmt->fetch()) {
  70. if ($data[$index] != $column1) {
  71. printf("[011] Row %d, expecting %s/%s got %s/%s\n",
  72. $index + 1, gettype($data[$index]), $data[$index], gettype($column1), $column1);
  73. }
  74. var_dump($column1);
  75. $index++;
  76. }
  77. $stmt->close();
  78. if ($IS_MYSQLND) {
  79. /* Advantage mysqlnd - see above... */
  80. if (!($stmt = $link->prepare("SELECT 1 AS column1 UNION SELECT CAST('three' AS CHAR) UNION SELECT CAST(2 AS CHAR)")))
  81. printf("[012] [%d] %s\n", $link->errno, $link->error);
  82. $column1 = null;
  83. if (!$stmt->bind_result($column1) || !$stmt->execute())
  84. printf("[013] [%d] %s\n", $stmt->errno, $stmt->error);
  85. $index = 0;
  86. while ($stmt->fetch()) {
  87. if ($data[$index] != $column1) {
  88. printf("[014] Row %d, expecting %s/%s got %s/%s\n",
  89. $index + 1, gettype($data[$index]), $data[$index], gettype($column1), $column1);
  90. }
  91. $index++;
  92. }
  93. $stmt->close();
  94. }
  95. print "Using integer only...\n";
  96. if (!($res = $link->query("SELECT 1 AS column1 UNION SELECT 303 UNION SELECT 2")))
  97. printf("[015] [%d] %s\n", $link->errno, $link->error);
  98. $data = array();
  99. while ($row = $res->fetch_assoc()) {
  100. $data[] = $row['column1'];
  101. }
  102. $res->free();
  103. // Prepared Statements
  104. if (!($stmt = $link->prepare("SELECT 1 AS column1 UNION SELECT 303 UNION SELECT 2")))
  105. printf("[016] [%d] %s\n", $link->errno, $link->error);
  106. $column1 = null;
  107. if (!$stmt->execute() || !$stmt->bind_result($column1))
  108. printf("[017] [%d] %s\n", $stmt->errno, $stmt->error);
  109. $index = 0;
  110. while ($stmt->fetch()) {
  111. if ($data[$index] != $column1) {
  112. printf("[018] Row %d, expecting %s/%s got %s/%s\n",
  113. $index + 1, gettype($data[$index]), $data[$index], gettype($column1), $column1);
  114. }
  115. var_dump($column1);
  116. $index++;
  117. }
  118. $stmt->close();
  119. if ($IS_MYSQLND) {
  120. /* Advantage mysqlnd - see above */
  121. if (!($stmt = $link->prepare("SELECT 1 AS column1 UNION SELECT 303 UNION SELECT 2")))
  122. printf("[019] [%d] %s\n", $link->errno, $link->error);
  123. $column1 = null;
  124. if (!$stmt->bind_result($column1) || !$stmt->execute())
  125. printf("[020] [%d] %s\n", $stmt->errno, $stmt->error);
  126. $index = 0;
  127. while ($stmt->fetch()) {
  128. if ($data[$index] != $column1) {
  129. printf("[021] Row %d, expecting %s/%s got %s/%s\n",
  130. $index + 1, gettype($data[$index]), $data[$index], gettype($column1), $column1);
  131. }
  132. $index++;
  133. }
  134. $stmt->close();
  135. }
  136. print "Testing bind_param(), strings only...\n";
  137. $two = 'two';
  138. $three = 'three';
  139. if (!($stmt = $link->prepare("SELECT 'one' AS column1 UNION SELECT ? UNION SELECT ?")))
  140. printf("[022] [%d] %s\n", $stmt->errno, $stmt->error);
  141. $column1 = null;
  142. if (!$stmt->bind_param('ss', $three, $two) || !$stmt->execute() || !$stmt->bind_result($column1))
  143. printf("[023] [%d] %s\n", $stmt->errno, $stmt->error);
  144. $index = 0;
  145. $data = array();
  146. while ($stmt->fetch()) {
  147. $data[$index++] = $column1;
  148. var_dump($column1);
  149. }
  150. $stmt->close();
  151. if ($IS_MYSQLND) {
  152. /* Advantage mysqlnd - see above */
  153. $two = 'two';
  154. $three = 'three';
  155. if (!($stmt = $link->prepare("SELECT 'one' AS column1 UNION SELECT ? UNION SELECT ?")))
  156. printf("[024] [%d] %s\n", $stmt->errno, $stmt->error);
  157. $column1 = null;
  158. if (!$stmt->bind_param('ss', $three, $two) || !$stmt->bind_result($column1) || !$stmt->execute())
  159. printf("[025] [%d] %s\n", $stmt->errno, $stmt->error);
  160. $index = 0;
  161. while ($stmt->fetch()) {
  162. if ($data[$index] != $column1) {
  163. printf("[26] Row %d, expecting %s/%s, got %s/%s\n",
  164. $index + 1, gettype($data[$index]), $data[$index], gettype($column1), $column1);
  165. }
  166. $index++;
  167. }
  168. $stmt->close();
  169. }
  170. print "Testing bind_param(), strings only, with CAST AS CHAR...\n";
  171. $two = 'two';
  172. $three = 'three beers are more than enough';
  173. if (!($stmt = $link->prepare("SELECT CAST('one' AS CHAR) AS column1 UNION SELECT CAST(? AS CHAR) UNION SELECT CAST(? AS CHAR)")))
  174. printf("[027] [%d] %s\n", $stmt->errno, $stmt->error);
  175. $column1 = null;
  176. if (!$stmt->bind_param('ss', $three, $two) || !$stmt->execute() || !$stmt->bind_result($column1))
  177. printf("[028] [%d] %s\n", $stmt->errno, $stmt->error);
  178. $index = 0;
  179. $data = array();
  180. while ($stmt->fetch()) {
  181. $data[$index++] = $column1;
  182. var_dump($column1);
  183. }
  184. $stmt->close();
  185. if ($IS_MYSQLND) {
  186. /* Advantage mysqlnd - see above */
  187. $two = 'two';
  188. $three = 'three beers are more than enough';
  189. if (!($stmt = $link->prepare("SELECT CAST('one' AS CHAR) AS column1 UNION SELECT CAST(? AS CHAR) UNION SELECT CAST(? AS CHAR)")))
  190. printf("[029] [%d] %s\n", $stmt->errno, $stmt->error);
  191. $column1 = null;
  192. if (!$stmt->bind_param('ss', $three, $two) || !$stmt->bind_result($column1) || !$stmt->execute())
  193. printf("[030] [%d] %s\n", $stmt->errno, $stmt->error);
  194. $index = 0;
  195. while ($stmt->fetch()) {
  196. if ($data[$index] != $column1) {
  197. printf("[31] Row %d, expecting %s/%s, got %s/%s\n",
  198. $index + 1, gettype($data[$index]), $data[$index], gettype($column1), $column1);
  199. }
  200. $index++;
  201. }
  202. $stmt->close();
  203. }
  204. $link->close();
  205. print "done!";
  206. ?>