xref: /PHP-7.4/ext/mysqli/tests/mysqli_query.phpt (revision e3e67b72)
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    if (false !== ($res = @mysqli_query($link, "SELECT id FROM test ORDER BY id", 1234)))
98        printf("[013] Invalid mode should return false got %s/%s, [%d] %s\n",
99            gettype($res), (is_object($res)) ? 'object' : $res,
100            mysqli_errno($link), mysqli_error($link));
101
102
103    mysqli_close($link);
104
105    if (false !== ($tmp = mysqli_query($link, "SELECT id FROM test")))
106        printf("[011] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
107
108    print "done!";
109?>
110--CLEAN--
111<?php
112require_once("connect.inc");
113if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
114   printf("[c001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
115
116if (!mysqli_query($link, "DROP TABLE IF EXISTS test"))
117    printf("[c002] Cannot drop table, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
118
119@mysqli_query($link, "DROP FUNCTION IF EXISTS f");
120@mysqli_query($link, 'DROP PROCEDURE IF EXISTS p');
121
122mysqli_close($link);
123?>
124--EXPECTF--
125array(1) {
126  ["valid"]=>
127  string(30) "this is sql but with semicolon"
128}
129array(1) {
130  [""]=>
131  string(1) "a"
132}
133string(1) "a"
134
135Warning: mysqli_query(): Couldn't fetch mysqli in %s on line %d
136done!
137