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