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