mysqli_multi_query.phpt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. --TEST--
  2. mysqli_multi_query()
  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. $strict_on = false;
  13. if (defined('E_STRICT')) {
  14. error_reporting(((int)ini_get('error_reporting')) | E_STRICT );
  15. $strict_on = true;
  16. }
  17. $tmp = NULL;
  18. $link = NULL;
  19. if (!is_null($tmp = @mysqli_multi_query()))
  20. printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  21. if (!is_null($tmp = @mysqli_multi_query($link)))
  22. printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  23. require('table.inc');
  24. if (false !== ($tmp = mysqli_multi_query($link, "")))
  25. printf("[003] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
  26. if (!mysqli_multi_query($link, "SELECT 1 AS a; SELECT 1 AS a, 2 AS b; SELECT id FROM test ORDER BY id LIMIT 3"))
  27. printf("[005] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  28. if ($strict_on)
  29. ob_start();
  30. $i = 0;
  31. do {
  32. $res = mysqli_store_result($link);
  33. while ($row = mysqli_fetch_array($res))
  34. ;
  35. mysqli_free_result($res);
  36. $i++;
  37. } while (mysqli_next_result($link));
  38. if ($strict_on) {
  39. $tmp = ob_get_contents();
  40. ob_end_clean();
  41. if (!preg_match('@Strict Standards: mysqli_next_result\(\): There is no next result set@ismU', $tmp)) {
  42. printf("[005a] Strict Standards warning missing\n");
  43. } else {
  44. $tmp = trim(preg_replace('@Strict Standards: mysqli_next_result\(\).*on line \d+@ism', '', $tmp));
  45. }
  46. print trim($tmp) . "\n";
  47. }
  48. printf("[006] %d\n", $i);
  49. if (!mysqli_multi_query($link, "ALTER TABLE test MODIFY id INT AUTO_INCREMENT; INSERT INTO test(label) VALUES ('a'); SELECT id, label FROM test ORDER BY id"))
  50. printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  51. $i = 0;
  52. while (mysqli_next_result($link) && ($res = mysqli_store_result($link))) {
  53. while ($row = mysqli_fetch_array($res))
  54. ;
  55. mysqli_free_result($res);
  56. printf("%d/%d\n", $i, mysqli_insert_id($link));
  57. $i++;
  58. }
  59. printf("[008] %d\n", $i);
  60. if (!mysqli_multi_query($link, "SELECT id, label FROM test"))
  61. printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  62. if ($strict_on)
  63. ob_start();
  64. $i = 0;
  65. while (mysqli_next_result($link) && ($res = mysqli_store_result($link))) {
  66. while ($row = mysqli_fetch_array($res))
  67. $i++;
  68. mysqli_free_result($res);
  69. }
  70. if ($strict_on) {
  71. $tmp = ob_get_contents();
  72. ob_end_clean();
  73. if (!preg_match('@Strict Standards: mysqli_next_result\(\): There is no next result set@ismU', $tmp)) {
  74. printf("[009a] Strict Standards warning missing\n");
  75. } else {
  76. $tmp = trim(preg_replace('@Strict Standards: mysqli_next_result\(\).*on line \d+@ism', '', $tmp));
  77. }
  78. print trim($tmp) . "\n";
  79. }
  80. printf("[010] %d\n", $i);
  81. if (!mysqli_multi_query($link, "SELECT 1 AS num, 'a' AS somechar; SELECT 2 AS num, 'a' AS somechar; SELECT 3 AS num, 'a' AS somechar"))
  82. printf("[011] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  83. $res_num = 1;
  84. do {
  85. if (!$res = mysqli_store_result($link)) {
  86. printf("[012 - %d] [%d] %s\n", $res_num, mysqli_errno($link), mysqli_error($link));
  87. continue;
  88. }
  89. $num_rows = 0;
  90. while ($row = mysqli_fetch_array($res)) {
  91. $num_rows++;
  92. if ($row['num'] != $res_num)
  93. printf("[013 - %d] Expecting %s got %s\n", $res_num, $res_num, $row['num']);
  94. if ($row['somechar'] != "a")
  95. printf("[014 - %d] Expecting a got %s\n", $res_num, $row['somechar']);
  96. if (1 == $num_rows) {
  97. /* simple metadata check */
  98. if (!($lengths = mysqli_fetch_lengths($res)))
  99. printf("[015 - %d] [%d] %s\n", $res_num, mysqli_errno($link), mysqli_error($link));
  100. if (count($lengths) != 2)
  101. printf("[016 - %d] Expecting 2 column lengths got %d [%d] %s\n", $res_num, count($lengths));
  102. foreach ($lengths as $k => $length)
  103. if ($length <= 0)
  104. printf("[017 - %d] Strange column lengths for column %d, got %d expecting any > 0\n",
  105. $res_num, $k, $length);
  106. }
  107. }
  108. if ($num_rows != 1)
  109. printf("[018 - %d] Expecting 1 row, got %d rows\n", $num_rows);
  110. $res_num++;
  111. mysqli_free_result($res);
  112. } while (@mysqli_next_result($link));
  113. if ($res_num != 4)
  114. printf("[015] Expecting 3 result sets got %d result set[s]\n", $res_num);
  115. mysqli_close($link);
  116. var_dump(mysqli_multi_query($link, "SELECT id, label FROM test"));
  117. print "done!";
  118. ?>
  119. --CLEAN--
  120. <?php
  121. require_once("clean_table.inc");
  122. ?>
  123. --EXPECTF--
  124. [006] 3
  125. [008] 0
  126. [009] [2014] Commands out of sync; you can't run this command now
  127. [010] 7
  128. Warning: mysqli_multi_query(): Couldn't fetch mysqli in %s on line %d
  129. NULL
  130. done!