The only similar question I have found is: Insert php boolean into mysql bit column with Zend_Db but this has no answer.
Please see below a simplified test:
The ‘allow’ column type is BIT.
The ‘roleID’ column type is INT.
The ‘permID’ column type is INT.
$dbo = new PDO("mysql:dbname=database;host=127.0.0.1", "phpuser", "pass"); $query = $dbo->prepare("INSERT INTO ws_role_perms (allow, roleID, permID) VALUES (:allow, :roleID, :permID)"); $query->bindValue("allow", true, PDO::PARAM_BOOL); $query->bindValue("roleID", 1, PDO::PARAM_STR); $query->bindValue("permID", 2, PDO::PARAM_STR); if ($query->execute() == false) { throw new Exception(print_r($query->errorInfo(), true)); }
The error message I receive is:
Array ( [0] => 22001 [1] => 1406 [2] => Data too long for column 'allow' at row 1 )
If I attempt to put the equivalent query straight into MySQL, i.e. by running the query: INSERT INTO ws_role_perms (allow, roleID, permID) VALUES (true, 1, 2)
I have no problems.
Is this a bug in the MySQL PDO driver, or is it just me?
Many thanks in anticipation of your help.
Answer
It seems as though PHP is not interpreting ‘true’ as a boolean. PHP.NET indicates that PDO::PARAM_BOOL is an integer. The MySQL interpretation of a column with datatype ‘bit’ changed after around version 5.0. What this means is that PHP is probably trying to insert a full integer (32 bits) into a column that has an unpredictable number of bits (1-64).