[php-src] Issue #11283: PDO converts boolean false to ''
| From: | MattyTheMad | Date: | Sat, 20 May 2023 08:10:02 +0000 |
| Subject: | [php-src] Issue #11283: PDO converts boolean false to '' | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-244491@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/11283
Author: MattyTheMad
### Description
The following code:
```php
$sql = 'INSERT INTO geo (
isError, isReview) VALUES (?,?);';
$p = [
0 => false,
1 => false,
];
$statement = \Teng\PDO::get()->prepare($sql);
$statement->execute($p);
```
Resulted in this output:
```
PHP Fatal error: Uncaught PDOException: SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect
integer value: '' for column z.geo.isError at row 1
```
But I expected this output instead:
```
Success
```
PDO converts boolean false to a zero-length string before trying to insert ''. MySQL will
not accept this as a boolean, which it considers to be a TINYINT (0 or 1).
This is super unintuitive and should be changed.
To get around this, I have to:
```php
$sql = 'INSERT INTO geo (isError, isReview) VALUES (?,?);';
$p = [
0 => false,
1 => false,
];
$statement = \Teng\PDO::get()->prepare($sql);
foreach ($p as $key => $value) {
if (is_bool($value)) {
$p[$key] = $value ? 1 : 0;
}
}
$statement->execute($p);
```
It may be too big a change to change FALSE from being equivalent to '' to 0 PHP wide, but
could we do a simple conversion, something like what I implemented here, inside PDO? Not being able
to pass a boolean value to what the database considers boolean is just a bit too silly for me.
### PHP Version
PHP 8.2.6
### Operating System
_No response_