mysqli_report.phpt 14 KB

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