|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2010-01-14 20:22 UTC] christian at activemediaworks dot com
 Description:
------------
When trying to insert a row with a 0-value into a column of type BIT(1), if the value for the column is not hard-coded into SQL statement, it causes the value to be inserted to become 1. If it is hardcoded, it is okay.
Reproduce code:
---------------
/* This results in the value of the column being set to 1 (incorrect) */
$db = new PDO ( "mysql:dbname=db1234;host=localhost", "user1234", "pass1234" );
$stmt = $db->prepare("INSERT INTO mytable (mybit) VALUES (?)");
$stmt->execute(array(0));
db_disconnect($db);
/* This results in the value of the column being set to 0 (correct) */
$db = new PDO ( "mysql:dbname=db1234;host=localhost", "user1234", "pass1234" );
$stmt = $db->prepare("INSERT INTO mytable (mybit) VALUES (0)");
$stmt->execute();
db_disconnect($db);
Actual result:
--------------
The first example inserts a row with the column value set to 1 (wrong)
The second example inserts a row with the column value set to 0 (right)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 09:00:01 2025 UTC | 
No bug here. This is how crappy PDO works ever since. If you don't specify a bind type, binding will use strings. Your code: $stmt = $db->prepare("INSERT INTO mytable (mybit) VALUES (?)"); $stmt->execute(array(0)); ... is equivalent to: $stmt = $db->prepare("INSERT INTO mytable (mybit) VALUES ('0')"); $stmt->execute(array(0)); Which in turn inserts 1 into the BIT column.