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