xref: /PHP-8.3/ext/mysqli/tests/bug34810.phpt (revision 23eb6a00)
1--TEST--
2Bug #34810 (mysqli::init() and others use wrong $this pointer without checks)
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once 'skipifconnectfailure.inc';
8?>
9--FILE--
10<?php
11
12class DbConnection {
13    public function connect() {
14        require_once 'connect.inc';
15
16        /* Pass false as $connect_flags cannot be accessed via globals. */
17        $link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket, false);
18        var_dump($link);
19
20        $link = mysqli_init();
21        var_dump($link);
22
23        $mysql = new my_mysqli($host, $user, $passwd, $db, $port, $socket, false);
24        $mysql->query("DROP TABLE IF EXISTS test_warnings");
25        $mysql->query("CREATE TABLE test_warnings (a int not null)");
26        $mysql->query("SET sql_mode=''");
27        $mysql->query("INSERT INTO test_warnings VALUES (1),(2),(NULL)");
28
29        $warning = $mysql->get_warnings();
30        if (!$warning)
31            printf("[001] No warning!\n");
32
33        if ($warning->errno == 1048 || $warning->errno == 1253) {
34            /* 1048 - Column 'a' cannot be null, 1263 - Data truncated; NULL supplied to NOT NULL column 'a' at row */
35            if ("HY000" != $warning->sqlstate)
36                printf("[003] Wrong sql state code: %s\n", $warning->sqlstate);
37
38            if ("" == $warning->message)
39                printf("[004] Message string must not be empty\n");
40        } else {
41            printf("[002] Empty error message!\n");
42            var_dump($warning);
43        }
44    }
45}
46
47$db = new DbConnection();
48$db->connect();
49
50echo "Done\n";
51?>
52--CLEAN--
53<?php
54require_once 'connect.inc';
55if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
56   printf("[c001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
57
58if (!mysqli_query($link, "DROP TABLE IF EXISTS test_warnings"))
59    printf("[c002] Cannot drop table, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
60
61mysqli_close($link);
62?>
63--EXPECTF--
64object(mysqli)#%d (%d) {
65  ["affected_rows"]=>
66  int(0)
67  ["client_info"]=>
68  string(%d) "%s"
69  ["client_version"]=>
70  int(%d)
71  ["connect_errno"]=>
72  int(0)
73  ["connect_error"]=>
74  NULL
75  ["errno"]=>
76  int(0)
77  ["error"]=>
78  string(0) ""
79  ["error_list"]=>
80  array(0) {
81  }
82  ["field_count"]=>
83  int(0)
84  ["host_info"]=>
85  string(%d) "%s"
86  ["info"]=>
87  %s
88  ["insert_id"]=>
89  int(0)
90  ["server_info"]=>
91  string(%d) "%s"
92  ["server_version"]=>
93  int(%d)
94  ["sqlstate"]=>
95  string(5) "00000"
96  ["protocol_version"]=>
97  int(10)
98  ["thread_id"]=>
99  int(%d)
100  ["warning_count"]=>
101  int(0)
102}
103object(mysqli)#%d (%d) {
104  ["client_info"]=>
105  string(%d) "%s"
106  ["client_version"]=>
107  int(%d)
108  ["connect_errno"]=>
109  int(0)
110  ["connect_error"]=>
111  NULL
112  ["errno"]=>
113  int(0)
114  ["error"]=>
115  string(0) ""
116}
117Done
118