Skip to content
Advertisement

Why is PDO converting my bool(false) param to string(”)?

I have a problem with a new MariaDB 10.5.8 install. STRICT_TRANS_TABLES is set, and when I try to use $sql of:

JavaScript

(where flag is defined as tinyint(1)) with var_dump($params) showing as:

JavaScript

I get this message:

JavaScript

If, instead, I do:

JavaScript

with no parameters, it works as expected.

This is how I connect to the database:

JavaScript

and this is how I send the query/params to MariaDB:

JavaScript

If I try to insert true instead of false, everything works fine. true is being converted to 1 somewhere along the lines, and false to ''. What am I doing wrong?

Advertisement

Answer

All values are treated as PDO::PARAM_STR.

https://www.php.net/manual/en/pdostatement.execute.php

The execute() method does not enable you to specify the variable’s type. It always casts to a string, which is why you’re getting strange results:

JavaScript

You can use PDOStatement::bindValue() to specify the value’s type:

JavaScript

Note that MySQL lacks a ‘true’ boolean type, which is why I’d recommend PDO::PARAM_INT over PDO::PARAM_BOOL.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement