1--TEST--
2mysqli_real_query()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7?>
8--FILE--
9<?php
10	require_once("connect.inc");
11
12	$tmp    = NULL;
13	$link   = NULL;
14
15	if (!is_null($tmp = @mysqli_real_query()))
16		printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
17
18	if (!is_null($tmp = @mysqli_real_query($link)))
19		printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
20
21	require('table.inc');
22
23	if (NULL !== ($tmp = @mysqli_real_query($link, "SELECT 1 AS a", MYSQLI_USE_RESULT, "foo")))
24		printf("[003] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
25
26	if (false !== ($tmp = mysqli_real_query($link, 'THIS IS NOT SQL')))
27		printf("[004] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
28
29	if (false !== ($tmp = mysqli_real_query($link, "SELECT 'this is sql but with backslash g'\g")))
30		printf("[005] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
31
32	if ((0 === mysqli_errno($link)) || ('' == mysqli_error($link)))
33		printf("[006] mysqli_errno()/mysqli_error should return some error\n");
34
35	if (!mysqli_real_query($link, "SELECT 'this is sql but with semicolon' AS valid ; "))
36		printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
37
38	if (!is_object($res = mysqli_use_result($link)))
39	printf("[008] Expecting reseult object, got %s/%s [%d] %s\n", gettype($res), $res, mysqli_errno($link), mysqli_error($link));
40
41	var_dump(mysqli_fetch_assoc($res));
42	mysqli_free_result($res);
43
44	if (false !== ($res = mysqli_real_query($link, "SELECT 'this is sql but with semicolon' AS valid ; SHOW VARIABLES")))
45		printf("[008] Expecting boolean/false, got %s/%s, [%d] %s\n", gettype($res), $res,
46			mysqli_errno($link), mysqli_error($link));
47
48	if (mysqli_get_server_version($link) > 50000) {
49		// let's try to play with stored procedures
50		mysqli_real_query($link, 'DROP PROCEDURE IF EXISTS p');
51		if (mysqli_real_query($link, 'CREATE PROCEDURE p(OUT ver_param VARCHAR(25)) BEGIN SELECT VERSION() INTO ver_param;
52END;')) {
53			mysqli_real_query($link, 'CALL p(@version)');
54			mysqli_real_query($link, 'SELECT @version AS p_version');
55			$res = mysqli_store_result($link);
56
57			$tmp = mysqli_fetch_assoc($res);
58			if (!is_array($tmp) || empty($tmp) || !isset($tmp['p_version']) || ('' == $tmp['p_version'])) {
59				printf("[008a] Expecting array [%d] %s\n", mysqli_errno($link), mysqli_error($link));
60				var_dump($tmp);
61			}
62
63			mysqli_free_result($res);
64		} else {
65				printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
66		}
67		mysqli_real_query($link, 'DROP FUNCTION IF EXISTS f');
68		if (mysqli_real_query($link, 'CREATE FUNCTION f( ver_param VARCHAR(25)) RETURNS VARCHAR(25) DETERMINISTIC RETURN
69ver_param;')) {
70			mysqli_real_query($link, 'SELECT f(VERSION()) AS f_version');
71			$res = mysqli_store_result($link);
72
73			$tmp = mysqli_fetch_assoc($res);
74			if (!is_array($tmp) || empty($tmp) || !isset($tmp['f_version']) || ('' == $tmp['f_version'])) {
75				printf("[009a] Expecting array [%d] %s\n", mysqli_errno($link), mysqli_error($link));
76				var_dump($tmp);
77			}
78
79			mysqli_free_result($res);
80		} else {
81			printf("[010] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
82		}
83	}
84
85	mysqli_close($link);
86
87	if (false !== ($tmp = mysqli_real_query($link, "SELECT id FROM test")))
88		printf("[011] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
89
90	print "done!";
91?>
92--CLEAN--
93<?php
94require_once("connect.inc");
95if (!$link = mysqli_connect($host, $user, $passwd, $db, $port, $socket))
96   printf("[c001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
97
98if (!mysqli_query($link, "DROP TABLE IF EXISTS test"))
99	printf("[c002] Cannot drop table, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
100
101@mysqli_query($link, "DROP PROCEDURE IF EXISTS p");
102@mysqli_query($link, "DROP FUNCTION IF EXISTS f");
103
104mysqli_close($link);
105?>
106--EXPECTF--
107array(1) {
108  ["valid"]=>
109  string(30) "this is sql but with semicolon"
110}
111
112Warning: mysqli_real_query(): Couldn't fetch mysqli in %s on line %d
113done!
114