xref: /PHP-8.4/ext/mysqli/tests/mysqli_query.phpt (revision 5853cdb7)
1--TEST--
2mysqli_query()
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once 'skipifconnectfailure.inc';
8?>
9--FILE--
10<?php
11    require 'table.inc';
12
13    try {
14        mysqli_query($link, '');
15    } catch (\ValueError $e) {
16        echo $e->getMessage() . \PHP_EOL;
17    }
18
19    if (false !== ($tmp = mysqli_query($link, 'THIS IS NOT SQL')))
20        printf("[004] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
21
22    if (false !== ($tmp = mysqli_query($link, "SELECT 'this is sql but with backslash g'\g")))
23        printf("[005] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
24
25    if ((0 === mysqli_errno($link)) || ('' == mysqli_error($link)))
26        printf("[006] mysqli_errno()/mysqli_error should return some error\n");
27
28    if (!$res = mysqli_query($link, "SELECT 'this is sql but with semicolon' AS valid ; "))
29        printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
30
31    var_dump(mysqli_fetch_assoc($res));
32    mysqli_free_result($res);
33
34    if (!$res = mysqli_query($link, "SELECT 'a' AS ''"))
35        printf("[007a] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
36
37    var_dump($tmp = mysqli_fetch_assoc($res));
38    var_dump($tmp[""]);
39    mysqli_free_result($res);
40
41    if (false !== ($res = mysqli_query($link, 'SELECT "this is sql but with semicolon" AS valid ; SHOW VARIABLES')))
42        printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
43
44    if (mysqli_get_server_version($link) > 50000) {
45        // let's try to play with stored procedures
46        mysqli_query($link, 'DROP PROCEDURE IF EXISTS p');
47        if (mysqli_query($link, 'CREATE PROCEDURE p(OUT ver_param VARCHAR(25)) BEGIN SELECT VERSION() INTO ver_param; END;')) {
48            mysqli_query($link, 'CALL p(@version)');
49            $res = mysqli_query($link, 'SELECT @version AS p_version');
50
51            $tmp = mysqli_fetch_assoc($res);
52            if (!is_array($tmp) || empty($tmp) || !isset($tmp['p_version']) || ('' == $tmp['p_version'])) {
53                printf("[008a] Expecting array [%d] %s\n", mysqli_errno($link), mysqli_error($link));
54                var_dump($tmp);
55            }
56
57            mysqli_free_result($res);
58        } else {
59            printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
60        }
61
62        mysqli_query($link, 'DROP FUNCTION IF EXISTS f');
63        if (mysqli_query($link, 'CREATE FUNCTION f( ver_param VARCHAR(25)) RETURNS VARCHAR(25) DETERMINISTIC RETURN ver_param;')) {
64            $res = mysqli_query($link, 'SELECT f(VERSION()) AS f_version');
65
66            $tmp = mysqli_fetch_assoc($res);
67            if (!is_array($tmp) || empty($tmp) || !isset($tmp['f_version']) || ('' == $tmp['f_version'])) {
68                printf("[009a] Expecting array [%d] %s\n", mysqli_errno($link), mysqli_error($link));
69                var_dump($tmp);
70            }
71
72            mysqli_free_result($res);
73        } else {
74            printf("[010] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
75        }
76    }
77
78    if (!is_object($res = mysqli_query($link, "SELECT id FROM test ORDER BY id", MYSQLI_USE_RESULT)))
79        printf("[011] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
80    mysqli_free_result($res);
81
82    if (!is_object($res = mysqli_query($link, "SELECT id FROM test ORDER BY id", MYSQLI_STORE_RESULT)))
83        printf("[012] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
84    mysqli_free_result($res);
85
86    try {
87        mysqli_query($link, "SELECT id FROM test ORDER BY id", 1234);
88    } catch (\ValueError $e) {
89        echo $e->getMessage() . \PHP_EOL;
90    }
91
92    mysqli_close($link);
93
94    try {
95        mysqli_query($link, "SELECT id FROM test");
96    } catch (Error $exception) {
97        echo $exception->getMessage() . "\n";
98    }
99
100    print "done!";
101?>
102--CLEAN--
103<?php
104require_once 'connect.inc';
105if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
106   printf("[c001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
107
108if (!mysqli_query($link, "DROP TABLE IF EXISTS test"))
109    printf("[c002] Cannot drop table, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
110
111@mysqli_query($link, "DROP FUNCTION IF EXISTS f");
112@mysqli_query($link, 'DROP PROCEDURE IF EXISTS p');
113
114mysqli_close($link);
115?>
116--EXPECTF--
117mysqli_query(): Argument #2 ($query) must not be empty
118array(1) {
119  ["valid"]=>
120  string(30) "this is sql but with semicolon"
121}
122array(1) {
123  [""]=>
124  string(1) "a"
125}
126string(1) "a"
127mysqli_query(): Argument #3 ($result_mode) must be either MYSQLI_USE_RESULT or MYSQLI_STORE_RESULT%S
128mysqli object is already closed
129done!
130