问题
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.
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...
Here's code:
<?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()));
Result:
array(4) {
["stmt result"]=>
bool(false)
["last insert id"]=>
string(1) "0"
["stmt err code"]=>
string(5) "00000"
["pdo err code"]=>
string(5) "00000"
}
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.
回答1:
I had the same problem on Ubuntu with PHP 5.3.10. (Interestingly there was no problem on windows with wamp...)
Actually it's a known bug in pdo: https://bugs.php.net/bug.php?id=38546
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.
来源:https://stackoverflow.com/questions/10242312/pdo-bindvalue-with-pdoparam-bool-causes-statement-execute-to-fail-silently