Misleading PDO error message ‹Invalid datetime format›
Description
While the MySQL error itself is expected (caused by the boolean value false for the second column), the error message Invalid datetime format is misleading since this has nothing to do with any date(time) types.
<?php
$pdo = new PDO('mysql:host=localhost;dbname=database_name;charset=utf8mb4', 'username', '...');
$handle = $pdo->prepare("INSERT INTO `test` (`true`, `false`) VALUES (?, ?)");
$handle->execute([true, false]);
Resulted in this output:
Uncaught PDOException: SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '' for column `test`.`test`.`false` [...]
SQL Structure
CREATE TABLE `test` (
`id` int(11) NOT NULL,
`true` tinyint(1) UNSIGNED NOT NULL,
`false` tinyint(1) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `test`
ADD PRIMARY KEY (`id`);
ALTER TABLE `test`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
PHP Version
PHP 8.0.14
Operating System
No response
Well, yeah it is, but that is exactly what the MySQL/MariaDB tells PDO:

If you check what is SQLSTATE 22007 you will see that it's "Invalid datetime format". https://en.wikipedia.org/wiki/SQLSTATE
Not really sure what PHP could do about that. We just convey the same wrong information that the database tells us.
Error numbers appear to be the same (1366 and 22007), but how come that the error message is different?
Error numbers appear to be the same (1366 and 22007), but how come that the error message is different?
I'm not sure what you mean. The messages are the same. It's just that PDO maps the SQLSTATE code additionally to its textual representation which makes the exception message be
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '' for column
test.test.false
Compare it to what MySQL console says:
ERROR 1366 (22007): Incorrect integer value: '' for column
test.test.false
It's the same message and same code.
For me this is not the same message.
For me this is not the same message.
Please expand on it. Why do you say it's not the same message? What would you expect to receive from PHP?
The error message is prepended by an additional error message Invalid datetime format, so the output is not the same.
Still, the error seems to come from MySQL as number 22007 refers to this message. PDO prepends this message, but this seems to be intended behaviour. However: Why are there two different error messages (from MySQL)?
Ahh ok, let me explain this better. When PHP (mysqlnd to be exact) receives response containing an error from the database server, it arrives with the following pieces of information:
Error message: "Incorrect integer value: '' for column `test`.`test`.`false` at row 1"
Error number: 1366
SQLSTATE: 22007
Now, it's a job of the client (PDO in this instance) to present the error message in the form of an exception. PDO has chosen to follow the following format for its exception messages:
SQLSTATE[<SQLSTATE>]: <SQLSTATE description>: <Error number> <Error message>
Other clients, such as MySQL console, have selected a different format. For MySQL console it is:
ERROR <Error number> (<SQLSTATE>): <Error message>
As you can see the format chosen is entirely different. MySQL console decided to not map the SQLSTATE to its description. PDO does. It's not an incorrect behaviour, it's just a different presentation method. The information being presented is the same and the error message is exactly as it comes from the database server.
I see, thanks. Thus, intended behaviour by PDO/PHP and to be reported on MariaDBs side 😊