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