2012-04-20 91 views
7

在一个服务器设置中,我遇到了非常奇怪的错误。有PHP 5.3.6与PDO驱动程序为MySQL,客户端库版本5.1.61。一切都是手工编写的。PDO bindValue与 PDO :: PARAM_BOOL导致语句执行失败默默

当我用bindValue绑定params并将第三个参数设置为\ PDO :: PARAM_BOOL时,语句执行返回false并且什么都没有发生(没有插入到MySQL的数据,甚至没有任何异常)。当我不使用第三个参数时,它很顺利。事实上,我不能ommit第三个参数,bacues Doctrine2 DBAL将它转换时的参数...

这里是代码:

<?php 
$pdo = new \PDO('mysql:host=***;dbname=***', '***', '***'); // hidden DB access 
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); 

$stmt = $pdo->prepare('insert into outage (name, description, start_at, end_at, is_exception, extranet_id) values (?,?,?,?,?,?)'); 
$stmt->bindValue(1, 'Test name', \PDO::PARAM_STR); 
$stmt->bindValue(2, 'Test desc', \PDO::PARAM_STR); 
$stmt->bindValue(3, '2012-01-01 00:00:00', \PDO::PARAM_STR); 
$stmt->bindValue(4, null, \PDO::PARAM_NULL); 
$stmt->bindValue(5, false, \PDO::PARAM_BOOL); 
$stmt->bindValue(6, 2, \PDO::PARAM_INT); 
var_dump(array('stmt result' => ($result = $stmt->execute()), 'last insert id' => $pdo->lastInsertId(), 'stmt err code' => $stmt->errorCode(), 'pdo err code' => $pdo->errorCode())); 

结果:

array(4) { 
    ["stmt result"]=> 
    bool(false) 
    ["last insert id"]=> 
    string(1) "0" 
    ["stmt err code"]=> 
    string(5) "00000" 
    ["pdo err code"]=> 
    string(5) "00000" 
} 

什么可能会错呢?我在其他4台服务器上试过了,没有得到这个错误。此外,如果我通过'0'(作为一个字符串)$stmt->bindValue(5, false, \PDO::PARAM_BOOL);它的效果很好。

+0

好的,这个服务器上的问题在PHP 5.3.10下解决(Red Hat 5.4) – 2012-04-20 08:41:14

回答

12

我在使用PHP 5.3.10的Ubuntu上遇到了同样的问题。 (有趣的是不存在任何问题与WAMP的窗口...)

其实它在PDO一个已知的bug:https://bugs.php.net/bug.php?id=38546

我用PDO :: PARAM_INT代替PDO的:: PARAM_BOOL。它运行良好,你不必像上面那样将布尔变换成字符串。