2012-04-20 94 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。它運行良好,你不必像上面那樣將布爾變換成字符串。