mysqli_report.phpt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. --TEST--
  2. mysqli_report()
  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 (NULL !== ($tmp = @mysqli_report()))
  15. printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  16. if (true !== ($tmp = mysqli_report(-1)))
  17. printf("[002] Expecting boolean/true even for invalid flags, got %s/%s\n", gettype($tmp), $tmp);
  18. if (true !== ($tmp = mysqli_report(MYSQLI_REPORT_ERROR)))
  19. printf("[003] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
  20. if (true !== ($tmp = mysqli_report(MYSQLI_REPORT_STRICT)))
  21. printf("[004] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
  22. if (true !== ($tmp = mysqli_report(MYSQLI_REPORT_INDEX)))
  23. printf("[005] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
  24. if (true !== ($tmp = mysqli_report(MYSQLI_REPORT_ALL)))
  25. printf("[007] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
  26. if (true !== ($tmp = mysqli_report(MYSQLI_REPORT_OFF)))
  27. printf("[008] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
  28. require('table.inc');
  29. /*
  30. Internal macro MYSQL_REPORT_ERROR
  31. */
  32. mysqli_report(MYSQLI_REPORT_ERROR);
  33. mysqli_multi_query($link, "BAR; FOO;");
  34. mysqli_query($link, "FOO");
  35. mysqli_kill($link, -1);
  36. // mysqli_ping() cannot be tested, because one would need to cause an error inside the C function to test it
  37. mysqli_prepare($link, "FOO");
  38. mysqli_real_query($link, "FOO");
  39. if (@mysqli_select_db($link, "Oh lord, let this be an unknown database name"))
  40. printf("[009] select_db should have failed\n");
  41. // mysqli_store_result() and mysqli_use_result() cannot be tested, because one would need to cause an error inside the C function to test it
  42. // Check that none of the above would have caused any error messages if MYSQL_REPORT_ERROR would
  43. // not have been set. If that would be the case, the test would be broken.
  44. mysqli_report(MYSQLI_REPORT_OFF);
  45. mysqli_multi_query($link, "BAR; FOO;");
  46. mysqli_query($link, "FOO");
  47. mysqli_kill($link, -1);
  48. mysqli_prepare($link, "FOO");
  49. mysqli_real_query($link, "FOO");
  50. mysqli_select_db($link, "Oh lord, let this be an unknown database name");
  51. /*
  52. Internal macro MYSQL_REPORT_STMT_ERROR
  53. */
  54. mysqli_report(MYSQLI_REPORT_ERROR);
  55. $stmt = mysqli_stmt_init($link);
  56. mysqli_stmt_prepare($stmt, "FOO");
  57. $stmt = mysqli_stmt_init($link);
  58. mysqli_stmt_prepare($stmt, "SELECT id FROM test WHERE id > ?");
  59. $id = 1;
  60. mysqli_kill($link, mysqli_thread_id($link));
  61. mysqli_stmt_bind_param($stmt, "i", $id);
  62. mysqli_stmt_close($stmt);
  63. mysqli_close($link);
  64. /* mysqli_stmt_execute() = mysql_stmt_execute cannot be tested from PHP */
  65. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  66. printf("[008] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
  67. $stmt = mysqli_stmt_init($link);
  68. mysqli_stmt_prepare($stmt, "SELECT id FROM test WHERE id > ?");
  69. $id = 1;
  70. mysqli_stmt_bind_param($stmt, "i", $id);
  71. // mysqli_kill($link, mysqli_thread_id($link));
  72. mysqli_stmt_execute($stmt);
  73. mysqli_stmt_close($stmt);
  74. mysqli_close($link);
  75. /* mysqli_kill() "trick" does not work for any of the following because of an E_COMMANDS_OUT_OF_SYNC */
  76. /* mysqli_stmt_bind_result() = mysql_stmt_bind_result() cannot be tested from PHP */
  77. /* mysqli_stmt_fetch() = mysql_stmt_fetch() cannot be tested from PHP */
  78. /* mysqli_stmt_result_metadata() = mysql_stmt_result_metadata() cannot be tested from PHP */
  79. /* mysqli_stmt_store_result() = mysql_stmt_store_result() cannot be tested from PHP */
  80. // Check
  81. mysqli_report(MYSQLI_REPORT_OFF);
  82. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  83. printf("[010] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
  84. $stmt = mysqli_stmt_init($link);
  85. mysqli_stmt_prepare($stmt, "FOO");
  86. $stmt = mysqli_stmt_init($link);
  87. mysqli_stmt_prepare($stmt, "SELECT id FROM test WHERE id > ?");
  88. $id = 1;
  89. mysqli_kill($link, mysqli_thread_id($link));
  90. mysqli_stmt_bind_param($stmt, "i", $id);
  91. mysqli_stmt_close($stmt);
  92. mysqli_close($link);
  93. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  94. printf("[011] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
  95. $stmt = mysqli_stmt_init($link);
  96. mysqli_stmt_prepare($stmt, "SELECT id FROM test WHERE id > ?");
  97. $id = 1;
  98. mysqli_stmt_bind_param($stmt, "i", $id);
  99. mysqli_kill($link, mysqli_thread_id($link));
  100. mysqli_stmt_execute($stmt);
  101. mysqli_stmt_close($stmt);
  102. mysqli_close($link);
  103. /*
  104. MYSQLI_REPORT_STRICT
  105. MYSQLI_REPORT_STRICT --->
  106. php_mysqli_report_error() ->
  107. MYSQLI_REPORT_MYSQL_ERROR,
  108. MYSQLI_REPORT_STMT_ERROR ->
  109. already tested
  110. php_mysqli_throw_sql_exception() ->
  111. my_mysqli_real_connect()
  112. my_mysqli_connect()
  113. can't be tested: mysqli_query() via mysql_use_result()/mysql_store_result()
  114. */
  115. mysqli_report(MYSQLI_REPORT_OFF);
  116. mysqli_report(MYSQLI_REPORT_STRICT);
  117. try {
  118. if ($link = my_mysqli_connect($host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket))
  119. printf("[012] Can connect to the server using host=%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n",
  120. $host, $user . 'unknown_really', $db, $port, $socket);
  121. mysqli_close($link);
  122. } catch (mysqli_sql_exception $e) {
  123. printf("[013] %s\n", $e->getMessage());
  124. }
  125. try {
  126. if (!$link = mysqli_init())
  127. printf("[014] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
  128. if ($link = my_mysqli_real_connect($link, $host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket))
  129. printf("[015] Can connect to the server using host=%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n",
  130. $host, $user . 'unknown_really', $db, $port, $socket);
  131. mysqli_close($link);
  132. } catch (mysqli_sql_exception $e) {
  133. printf("[016] %s\n", $e->getMessage());
  134. }
  135. /*
  136. MYSQLI_REPORT_INDEX --->
  137. mysqli_query()
  138. mysqli_stmt_execute()
  139. mysqli_prepare()
  140. mysqli_real_query()
  141. mysqli_store_result()
  142. mysqli_use_result()
  143. No test, because of to many prerequisites:
  144. - Server needs to be started with and
  145. --log-slow-queries --log-queries-not-using-indexes
  146. - query must cause the warning on all MySQL versions
  147. TODO:
  148. */
  149. $log_slow_queries = false;
  150. $log_queries_not_using_indexes = false;
  151. mysqli_report(MYSQLI_REPORT_OFF);
  152. mysqli_report(MYSQLI_REPORT_INDEX);
  153. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  154. printf("[017] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
  155. if (mysqli_get_server_version($link) <= 50600) {
  156. // this might cause a warning - no index used
  157. if (!$res = @mysqli_query($link, "SHOW VARIABLES LIKE 'log_slow_queries'"))
  158. printf("[018] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  159. if (!$row = mysqli_fetch_assoc($res))
  160. printf("[019] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  161. $log_slow_query = ('ON' == $row['Value']);
  162. if (mysqli_get_server_version($link) >= 50111) {
  163. // this might cause a warning - no index used
  164. if (!$res = @mysqli_query($link, "SHOW VARIABLES LIKE 'log_queries_not_using_indexes'"))
  165. printf("[020] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  166. if (!$row = mysqli_fetch_assoc($res))
  167. printf("[021] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  168. $log_queries_not_using_indexes = ('ON' == $row['Value']);
  169. if ($log_slow_queries && $log_queries_not_using_indexes) {
  170. for ($i = 100; $i < 20000; $i++) {
  171. if (!mysqli_query($link, "INSERT INTO test(id, label) VALUES ($i, 'z')"))
  172. printf("[022 - %d] [%d] %s\n", $i - 99, mysqli_errno($link), mysqli_error($link));
  173. }
  174. // this might cause a warning - no index used
  175. if (!$res = @mysqli_query($link, "SELECT id, label FROM test WHERE id = 1323"))
  176. printf("[023] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  177. mysqli_free_result($res);
  178. }
  179. }
  180. }
  181. // Maybe we've provoked an index message, maybe not.
  182. // All we can do is make a few dummy calls to ensure that all codes gets executed which
  183. // checks the flag. Functions to check: mysqli_query() - done above,
  184. // mysqli_stmt_execute(), mysqli_prepare(), mysqli_real_query(), mysqli_store_result()
  185. // mysqli_use_result(), mysqli_thread_safe(), mysqli_thread_id()
  186. mysqli_report(MYSQLI_REPORT_OFF);
  187. mysqli_close($link);
  188. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  189. printf("[024] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
  190. if (!$stmt = mysqli_stmt_init($link))
  191. printf("[025] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  192. if (!mysqli_stmt_prepare($stmt, 'SELECT id, label FROM test'))
  193. printf("[026] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
  194. if (!mysqli_stmt_execute($stmt))
  195. printf("[027] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
  196. mysqli_stmt_close($stmt);
  197. if (!mysqli_real_query($link, 'SELECT label, id FROM test'))
  198. printf("[028] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  199. if (!$res = mysqli_use_result($link))
  200. printf("[029] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  201. mysqli_free_result($res);
  202. if (!mysqli_real_query($link, 'SELECT label, id FROM test'))
  203. printf("[030] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  204. if (!$res = mysqli_store_result($link))
  205. printf("[031] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  206. mysqli_free_result($res);
  207. if (!$stmt = mysqli_prepare($link, 'SELECT id * 3 FROM test'))
  208. printf("[032] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  209. else
  210. mysqli_stmt_close($stmt);
  211. if (!mysqli_query($link, "INSERT INTO test(id, label) VALUES (100, 'z')", MYSQLI_USE_RESULT) ||
  212. !mysqli_query($link, 'DELETE FROM test WHERE id > 50', MYSQLI_USE_RESULT))
  213. printf("[033] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  214. $tmp = mysqli_thread_safe($link);
  215. $tmp = mysqli_thread_id($link);
  216. mysqli_close($link);
  217. print "done!";
  218. ?>
  219. --CLEAN--
  220. <?php
  221. require_once("clean_table.inc");
  222. ?>
  223. --EXPECTF--
  224. Warning: mysqli_multi_query(): (%d/%d): You have an error in your SQL syntax; check the manual that corresponds to your %s server version for the right syntax to use near 'BAR; FOO' at line 1 in %s on line %d
  225. Warning: mysqli_query(): (%d/%d): You have an error in your SQL syntax; check the manual that corresponds to your %s server version for the right syntax to use near 'FOO' at line 1 in %s on line %d
  226. Warning: mysqli_kill(): processid should have positive value in %s on line %d
  227. Warning: mysqli_prepare(): (%d/%d): You have an error in your SQL syntax; check the manual that corresponds to your %s server version for the right syntax to use near 'FOO' at line 1 in %s on line %d
  228. Warning: mysqli_real_query(): (%d/%d): You have an error in your SQL syntax; check the manual that corresponds to your %s server version for the right syntax to use near 'FOO' at line 1 in %s on line %d
  229. Warning: mysqli_kill(): processid should have positive value in %s on line %d
  230. Warning: mysqli_stmt_prepare(): (%d/%d): You have an error in your SQL syntax; check the manual that corresponds to your %s server version for the right syntax to use near 'FOO' at line 1 in %s on line %d
  231. [013] Access denied for user '%s'@'%s' (using password: YES)
  232. [016] Access denied for user '%s'@'%s' (using password: YES)
  233. done!