具有\ PDO :: PARAM_BOOL的PDO bindValue导致语句执行静默失败 [英] PDO bindValue with \PDO::PARAM_BOOL causes statement execute to fail silently

查看:54
本文介绍了具有\ PDO :: PARAM_BOOL的PDO bindValue导致语句执行静默失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个服务器设置中,我遇到一个非常奇怪的错误.有适用于MySQL的PHP​​ 5.3.6和PDO驱动程序,客户端库版本为5.1.61.一切都是手工编译的.

In one server setup I experience very strange error. There's PHP 5.3.6 with PDO Driver for MySQL, client library version 5.1.61. Everything is compiled by hand.

当我用bindValue绑定参数并将第三个参数设置为\ PDO :: PARAM_BOOL时,语句execute返回false并且什么也没有发生(没有数据插入MySQL,甚至根本没有异常).当我不使用第三个参数时,它运行得很好.实际上,我不能省略第三个参数,Bacues Doctrine2 DBAL在转换参数时对其进行了设置...

When I bind params with bindValue and set third parameter as \PDO::PARAM_BOOL then statement execute return false and nothing happens (no data inserted to MySQL, even no exception at all). When I don't use third parameter it goes well. In fact I can't ommit third parameter, bacues Doctrine2 DBAL sets it while converting parameters...

这里的代码:

<?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); 效果很好.

What possibly could go wrong? I've tried it on other 4 servers and didn't get this bug. Also if I pass '0' (as a string) to $stmt->bindValue(5, false, \PDO::PARAM_BOOL); it works good.

推荐答案

在使用PHP 5.3.10的Ubuntu上,我遇到了同样的问题.(有趣的是,在带有沼泽的窗户上没有问题...)

I had the same problem on Ubuntu with PHP 5.3.10. (Interestingly there was no problem on windows with wamp...)

实际上,这是pdo中的一个已知错误: https://bugs.php.net/bug.php?id= 38546

Actually it's a known bug in pdo: https://bugs.php.net/bug.php?id=38546

我使用PDO :: PARAM_INT而不是PDO :: PARAM_BOOL.它运作良好,并且您不必像上面那样将布尔值转换为字符串.

I use PDO::PARAM_INT instead of PDO::PARAM_BOOL. It works well, and you do not have to convert booleans to string like above.

这篇关于具有\ PDO :: PARAM_BOOL的PDO bindValue导致语句执行静默失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆