xref: /PHP-5.6/ext/mysqli/tests/mysqli_query.phpt (revision eb0e09f5)
1--TEST--
2mysqli_query()
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 (!is_null($tmp = @mysqli_query()))
17		printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
18
19	if (!is_null($tmp = @mysqli_query($link)))
20		printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
21
22	require('table.inc');
23
24	if (false !== ($tmp = @mysqli_query($link, '')))
25		printf("[002a] Expecting boolean/false got %s/%s\n", gettype($tmp), $tmp);
26
27	if (NULL !== ($tmp = @mysqli_query($link, "SELECT 1 AS a", MYSQLI_USE_RESULT, "foo")))
28		printf("[003] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
29
30	if (false !== ($tmp = mysqli_query($link, 'THIS IS NOT SQL')))
31		printf("[004] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
32
33	if (false !== ($tmp = mysqli_query($link, "SELECT 'this is sql but with backslash g'\g")))
34		printf("[005] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
35
36	if ((0 === mysqli_errno($link)) || ('' == mysqli_error($link)))
37		printf("[006] mysqli_errno()/mysqli_error should return some error\n");
38
39	if (!$res = mysqli_query($link, "SELECT 'this is sql but with semicolon' AS valid ; "))
40		printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
41
42	var_dump(mysqli_fetch_assoc($res));
43	mysqli_free_result($res);
44
45	if (!$res = mysqli_query($link, "SELECT 'a' AS ''"))
46		printf("[007a] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
47
48	var_dump($tmp = mysqli_fetch_assoc($res));
49	var_dump($tmp[""]);
50	mysqli_free_result($res);
51
52	if (false !== ($res = mysqli_query($link, 'SELECT "this is sql but with semicolon" AS valid ; SHOW VARIABLES')))
53		printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
54
55	if (mysqli_get_server_version($link) > 50000) {
56		// let's try to play with stored procedures
57		mysqli_query($link, 'DROP PROCEDURE IF EXISTS p');
58		if (mysqli_query($link, 'CREATE PROCEDURE p(OUT ver_param VARCHAR(25)) BEGIN SELECT VERSION() INTO ver_param; END;')) {
59			$res = mysqli_query($link, 'CALL p(@version)');
60			$res = mysqli_query($link, 'SELECT @version AS p_version');
61
62			$tmp = mysqli_fetch_assoc($res);
63			if (!is_array($tmp) || empty($tmp) || !isset($tmp['p_version']) || ('' == $tmp['p_version'])) {
64				printf("[008a] Expecting array [%d] %s\n", mysqli_errno($link), mysqli_error($link));
65				var_dump($tmp);
66			}
67
68			mysqli_free_result($res);
69		} else {
70			printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
71		}
72
73		mysqli_query($link, 'DROP FUNCTION IF EXISTS f');
74		if (mysqli_query($link, 'CREATE FUNCTION f( ver_param VARCHAR(25)) RETURNS VARCHAR(25) DETERMINISTIC RETURN ver_param;')) {
75			$res = mysqli_query($link, 'SELECT f(VERSION()) AS f_version');
76
77			$tmp = mysqli_fetch_assoc($res);
78			if (!is_array($tmp) || empty($tmp) || !isset($tmp['f_version']) || ('' == $tmp['f_version'])) {
79				printf("[009a] Expecting array [%d] %s\n", mysqli_errno($link), mysqli_error($link));
80				var_dump($tmp);
81			}
82
83			mysqli_free_result($res);
84		} else {
85			printf("[010] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
86		}
87	}
88
89	if (!is_object($res = mysqli_query($link, "SELECT id FROM test ORDER BY id", MYSQLI_USE_RESULT)))
90		printf("[011] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
91	mysqli_free_result($res);
92
93	if (!is_object($res = mysqli_query($link, "SELECT id FROM test ORDER BY id", MYSQLI_STORE_RESULT)))
94		printf("[012] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
95	mysqli_free_result($res);
96
97	$valid = array(MYSQLI_USE_RESULT, MYSQLI_STORE_RESULT);
98	do {
99		$mode = mt_rand(-1000, 1000);
100	} while (in_array($mode, $valid));
101
102	if (false !== ($res = @mysqli_query($link, "SELECT id FROM test ORDER BY id", $mode)))
103		printf("[013] Invalid mode should return false got %s/%s, [%d] %s\n",
104			gettype($res), (is_object($res)) ? 'object' : $res,
105			mysqli_errno($link), mysqli_error($link));
106
107
108	mysqli_close($link);
109
110	if (NULL !== ($tmp = mysqli_query($link, "SELECT id FROM test")))
111		printf("[011] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
112
113	print "done!";
114?>
115--CLEAN--
116<?php
117require_once("connect.inc");
118if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
119   printf("[c001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
120
121if (!mysqli_query($link, "DROP TABLE IF EXISTS test"))
122	printf("[c002] Cannot drop table, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
123
124@mysqli_query($link, "DROP FUNCTION IF EXISTS f");
125@mysqli_query($link, 'DROP PROCEDURE IF EXISTS p');
126
127mysqli_close($link);
128?>
129--EXPECTF--
130array(1) {
131  [%u|b%"valid"]=>
132  %unicode|string%(30) "this is sql but with semicolon"
133}
134array(1) {
135  [%u|b%""]=>
136  %unicode|string%(1) "a"
137}
138%unicode|string%(1) "a"
139
140Warning: mysqli_query(): Couldn't fetch mysqli in %s on line %d
141done!