xref: /PHP-8.2/ext/pdo_mysql/tests/bug_38546.phpt (revision 39131219)
1--TEST--
2PDO MySQL Bug #38546 (bindParam incorrect processing of bool types)
3--EXTENSIONS--
4pdo
5pdo_mysql
6--SKIPIF--
7<?php
8require dirname(__FILE__) . '/config.inc';
9require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
10PDOTest::skip();
11?>
12--FILE--
13<?php
14require dirname(__FILE__) . '/config.inc';
15require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
16$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
17
18$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
19
20// To test error cases.
21$db->exec("SET sql_mode='STRICT_TRANS_TABLES'");
22
23$db->exec("DROP TABLE IF EXISTS test");
24
25$query = "CREATE TABLE test(
26            uid MEDIUMINT UNSIGNED NOT NULL,
27            some_bool_1 BOOL NOT NULL,
28            some_bool_2 BOOL NOT NULL,
29            some_int TINYINT NOT NULL
30        )";
31$db->exec($query);
32
33$st = $db->prepare("INSERT INTO test (uid, some_bool_1, some_bool_2, some_int) VALUES (?, ?, ?, ?)");
34
35$values = [
36    'uid' => 6,
37    'some_bool_1' => false,
38    'some_bool_2' => true,
39    'some_int' => -23
40];
41$st->bindParam(1, $values['uid'], PDO::PARAM_INT);
42$st->bindParam(2, $values['some_bool_1'], PDO::PARAM_BOOL);
43$st->bindParam(3, $values['some_bool_2'], PDO::PARAM_BOOL);
44$st->bindParam(4, $values['some_int'], PDO::PARAM_INT);
45
46$result = $st->execute();
47
48if ($result === false) {
49    var_dump($st->errorInfo());
50} else {
51    print("ok insert\n");
52}
53
54foreach ($db->query('SELECT * from test') as $row) {
55    print_r($row);
56}
57
58$st = $db->prepare("UPDATE test SET some_bool_1=?, some_bool_2=?, some_int=? WHERE uid=?");
59
60$values = [
61    'uid' => 6,
62    'some_bool_1' => (bool) 1,
63    'some_bool_2' => (bool) 0,
64    'some_int' => 1,
65];
66
67$st->bindParam(1, $values['some_bool_1'], PDO::PARAM_BOOL);
68$st->bindParam(2, $values['some_bool_2'], PDO::PARAM_BOOL);
69$st->bindParam(3, $values['some_int'], PDO::PARAM_INT);
70$st->bindParam(4, $values['uid'], PDO::PARAM_INT);
71
72$result = $st->execute();
73
74if ($result === false) {
75    var_dump($st->errorInfo());
76} else {
77    print("ok prepare 1\n");
78}
79
80foreach ($db->query('SELECT * from test') as $row) {
81    print_r($row);
82}
83
84$st = $db->prepare("UPDATE test SET some_bool_1=?, some_bool_2=?, some_int=? WHERE uid=?");
85
86$values = [
87    'uid' => 6,
88    'some_bool_1' => (bool) 0,
89    'some_bool_2' => (bool) 1,
90    'some_int' => 2,
91];
92
93$st->bindParam(1, $values['some_bool_1'], PDO::PARAM_BOOL);
94$st->bindParam(2, $values['some_bool_2'], PDO::PARAM_BOOL);
95$st->bindParam(3, $values['some_int'], PDO::PARAM_INT);
96$st->bindParam(4, $values['uid'], PDO::PARAM_INT);
97
98$result = $st->execute();
99
100if ($result === false) {
101    var_dump($st->errorInfo());
102} else {
103    print("ok prepare 2\n");
104}
105
106foreach ($db->query('SELECT * from test') as $row) {
107    print_r($row);
108}
109
110// String true and false should fail
111$st = $db->prepare("UPDATE test SET some_bool_1=?, some_bool_2=?, some_int=? WHERE uid=?");
112
113$values = [
114    'uid' => 6,
115    'some_bool_1' => 'true',
116    'some_bool_2' => 'false',
117    'some_int' => 3,
118];
119
120$st->bindParam(1, $values['some_bool_1'], PDO::PARAM_BOOL);
121$st->bindParam(2, $values['some_bool_2'], PDO::PARAM_BOOL);
122$st->bindParam(3, $values['some_int'], PDO::PARAM_INT);
123$st->bindParam(4, $values['uid'], PDO::PARAM_INT);
124
125$result = $st->execute();
126
127if ($result === false) {
128    var_dump($st->errorInfo());
129} else {
130    print("ok prepare 3\n");
131}
132
133foreach ($db->query('SELECT * from test') as $row) {
134    print_r($row);
135}
136
137// Null should not be treated as false
138$st = $db->prepare("UPDATE test SET some_bool_1=?, some_bool_2=?, some_int=? WHERE uid=?");
139
140$values = [
141    'uid' => 6,
142    'some_bool_1' => true,
143    'some_bool_2' => null,
144    'some_int' => 4,
145];
146
147$st->bindParam(1, $values['some_bool_1'], PDO::PARAM_BOOL);
148$st->bindParam(2, $values['some_bool_2'], PDO::PARAM_BOOL);
149$st->bindParam(3, $values['some_int'], PDO::PARAM_INT);
150$st->bindParam(4, $values['uid'], PDO::PARAM_INT);
151
152$result = $st->execute();
153
154if ($result === false) {
155    var_dump($st->errorInfo());
156} else {
157    print("ok prepare 4\n");
158}
159
160foreach ($db->query('SELECT * from test') as $row) {
161    print_r($row);
162}
163
164// Integers converted correctly
165$st = $db->prepare("UPDATE test SET some_bool_1=?, some_bool_2=?, some_int=? WHERE uid=?");
166
167$values = [
168    'uid' => 6,
169    'some_bool_1' => 256,
170    'some_bool_2' => 0,
171    'some_int' => 5,
172];
173
174$st->bindParam(1, $values['some_bool_1'], PDO::PARAM_BOOL);
175$st->bindParam(2, $values['some_bool_2'], PDO::PARAM_BOOL);
176$st->bindParam(3, $values['some_int'], PDO::PARAM_INT);
177$st->bindParam(4, $values['uid'], PDO::PARAM_INT);
178
179$result = $st->execute();
180
181if ($result === false) {
182    var_dump($st->errorInfo());
183} else {
184    print("ok prepare 5\n");
185}
186
187foreach ($db->query('SELECT * from test') as $row) {
188    print_r($row);
189}
190
191?>
192--CLEAN--
193<?php
194require dirname(__FILE__) . '/mysql_pdo_test.inc';
195MySQLPDOTest::dropTestTable();
196?>
197--EXPECTF--
198ok insert
199Array
200(
201    [uid] => 6
202    [0] => 6
203    [some_bool_1] => 0
204    [1] => 0
205    [some_bool_2] => 1
206    [2] => 1
207    [some_int] => -23
208    [3] => -23
209)
210ok prepare 1
211Array
212(
213    [uid] => 6
214    [0] => 6
215    [some_bool_1] => 1
216    [1] => 1
217    [some_bool_2] => 0
218    [2] => 0
219    [some_int] => 1
220    [3] => 1
221)
222ok prepare 2
223Array
224(
225    [uid] => 6
226    [0] => 6
227    [some_bool_1] => 0
228    [1] => 0
229    [some_bool_2] => 1
230    [2] => 1
231    [some_int] => 2
232    [3] => 2
233)
234
235Warning: PDOStatement::execute(): SQLSTATE[%s]: %s: 1366 Incorrect integer value: 'true' for column %s at row 1 in %s
236array(3) {
237  [0]=>
238  string(5) "%s"
239  [1]=>
240  int(1366)
241  [2]=>
242  string(%d) "Incorrect integer value: 'true' for column %s at row 1"
243}
244Array
245(
246    [uid] => 6
247    [0] => 6
248    [some_bool_1] => 0
249    [1] => 0
250    [some_bool_2] => 1
251    [2] => 1
252    [some_int] => 2
253    [3] => 2
254)
255
256Warning: PDOStatement::execute(): SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'some_bool_2' cannot be null in %s
257array(3) {
258  [0]=>
259  string(5) "23000"
260  [1]=>
261  int(1048)
262  [2]=>
263  string(35) "Column 'some_bool_2' cannot be null"
264}
265Array
266(
267    [uid] => 6
268    [0] => 6
269    [some_bool_1] => 0
270    [1] => 0
271    [some_bool_2] => 1
272    [2] => 1
273    [some_int] => 2
274    [3] => 2
275)
276ok prepare 5
277Array
278(
279    [uid] => 6
280    [0] => 6
281    [some_bool_1] => 1
282    [1] => 1
283    [some_bool_2] => 0
284    [2] => 0
285    [some_int] => 5
286    [3] => 5
287)
288