|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2014-02-03 12:39 UTC] david at beroff dot com
Description: ------------ bindValue( ... PDO::PARAM_BOOL ) and bindParam( ... PDO::PARAM_BOOL ) appear to cause the subsequent execute() to not do anything, as well as to not report that the operation failed. Reported earlier, (2006), but suspended due to no feedback: https://bugs.php.net/bug.php?id=38386 https://bugs.php.net/bug.php?id=38546 Apparently may have already been fixed for Postgres: https://bugs.php.net/bug.php?id=62593 May also be related to: https://bugs.php.net/bug.php?id=41365 https://bugs.php.net/bug.php?id=57489 Software versions: Linux Version 2.6.32.39-grsec-3.mosso5.1.x86_64 PHP Version 5.3.20 (external host; unable to upgrade) PHP MySQL library version 5.0.77 MySQL server version 5.1.70 Test script: --------------- <html><body><pre><?php try { $db = new PDO( "mysql:host=HOSTNAME;dbname=DATABASE", "USERNAME", "PASSWORD", array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ) ); $db->exec( "DROP TABLE IF EXISTS TestPDO;" ); $db->exec( "CREATE TABLE TestPDO (ColT BOOLEAN, ColF BOOLEAN) ENGINE = InnoDB;" ); $stmt = $db->prepare( "INSERT INTO TestPDO (ColT, ColF) VALUES (:ColT, :ColF);" ); $stmt->bindValue( ":ColT", TRUE, PDO::PARAM_BOOL ); $stmt->bindValue( ":ColF", FALSE, PDO::PARAM_BOOL ); $stmt->execute(); // Suggested workaround (from bug #38546 [2007-04-11 15:26 UTC]) doesn't work: $colT = TRUE; $colF = FALSE; $stmt->bindParam( ":ColT", $colT, PDO::PARAM_BOOL ); $stmt->bindParam( ":ColF", $colF, PDO::PARAM_BOOL ); $stmt->execute(); // New proposed workaround: $stmt->bindValue( ":ColT", $colT ? 1 : 0, PDO::PARAM_INT ); $stmt->bindValue( ":ColF", $colF ? 1 : 0, PDO::PARAM_INT ); $stmt->execute(); foreach ($db->query( "SELECT * FROM TestPDO" ) as $row) print_r($row); $db = NULL; } catch (PDOException $e) { echo $e->getMessage(); } ?></pre></body></html> Expected result: ---------------- Array ( [ColT] => 1 [ColF] => 0 ) Array ( [ColT] => 1 [ColF] => 0 ) Array ( [ColT] => 1 [ColF] => 0 ) Actual result: -------------- Array ( [ColT] => 1 [ColF] => 0 ) PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 18:00:01 2025 UTC |
I recently ran into this same issue and was very surprised by the unexpected, broken behavior of PDO::PARAM_BOOL. I bumped into this issue after disabling PDO::ATTR_EMULATE_PREPARES and noticed many of unit tests were suddenly failing. So it is even more surprising that this works when emulating prepares, but fails when using real prepared statements. I have since changed all PDO::PARAM_BOOL to PDO::PARAM_INT in my code as a workaround. Below is the test case I used to demonstrate to myself the broken behavior. Test script: --- <?php $dns = "mysql:host=localhost;dbname=testdb"; $user = 'user'; $pass = 'pass'; $options = array( PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, ); $db = new PDO($dns, $user, $pass, $options); $db->query("DROP TABLE IF EXISTS booltest"); $db->query("CREATE TABLE booltest (val BOOLEAN)"); $db->query("INSERT INTO booltest (val) VALUES (FALSE)"); $stmt = $db->prepare("SELECT COUNT(*) FROM booltest WHERE val = ?"); $stmt->bindValue(1, false, PDO::PARAM_BOOL); $stmt->execute(); $count = $stmt->fetchColumn(); echo sprintf("count: %d\n", $count); $stmt = $db->prepare("SELECT COUNT(*) FROM booltest WHERE val = ?"); $stmt->bindValue(1, false, PDO::PARAM_INT); $stmt->execute(); $count = $stmt->fetchColumn(); echo sprintf("count: %d\n", $count); --- Expected --- count: 1 count: 1 --- Actual --- count: 0 count: 1 --- All tests are on Fedora 20. $ php -v PHP 5.5.18 (cli) (built: Oct 16 2014 13:13:55) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans $ mysql --version mysql Ver 14.14 Distrib 5.5.38, for Linux (x86_64) using readline 5.1