ID:               49255
 Comment by:       sjoerd-php at linuxonly dot nl
 Reported By:      carysmith at creighton dot edu
 Status:           Open
 Bug Type:         PDO related
 Operating System: Windows 2003 Ent.
 PHP Version:      5.2.10
 New Comment:

Thank you for your report.

When PDO replaces question marks in execute(), it does not take the
type of the columns into account. It thus does not know that the
parameter to execute() should be interpreted as a boolean, it simply
replaces question marks by strings. Because the string representation of
false is the empty string, it replaces the question mark by ''.

It makes no sense to change the string representation of false to 0,
because that would give problems on other places in PHP. It makes no
sense to parse the query to determine the type, as this is very hard to
do.

To work around this problem, you should convert the boolean value
yourself. You can do so like this:
$numeric_boolean = (int)$php_boolean;


Previous Comments:
------------------------------------------------------------------------

[2009-08-14 15:53:18] carysmith at creighton dot edu

Description:
------------
Before I report the bug, would just like to say that PDO is excellent
and I am enjoying working with it.

The Issue:
When attempting to insert a Boolean FALSE into a MySQL Boolean column,
the insert fails. Inserting a 0 does not. 

The following example code is borrowed from another post which was
similar http://bugs.php.net/bug.php?id=38386 and modified to accommodate
how I am using it.

Reproduce code:
---------------
<?php
$dbh = new PDO( 'mysql:host=localhost; dbname=Testing', 'DevUser',
'p...@ssw0rd!');

// Create table
$query = 'CREATE TABLE Testing.test_boolean(TheBoolean BOOLEAN);';
$createStatement = $dbh->prepare( $query);
$createStatement->execute();

// Attempt to insert Boolean records into table - (FALSE insert fails
without error)
$query = 'insert into Testing.test_boolean set TheBoolean = ?;';
$statement = $dbh->prepare( $query);
$statement->execute(array(true));
$statement->execute(array(false));

// However by replacing Boolean with integers does work
$statement->execute(array(1));
$statement->execute(array(0));
?>

Expected result:
----------------
4 rows added to the table:

1
0
1
0



Actual result:
--------------
3 rows added to the table:

1
1
0

Investigation of the MySQL logs shows the Boolean FALSE is inserted as
an empty string '' which fails. Please verify if I am missing something
or this cannot be reproduced. In the meantime I am changing the code to
use numbers instead of Boolean.


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=49255&edit=1

Reply via email to