2012-04-20 14 views
7

In una configurazione del server, si verifica un errore molto strano. C'è PHP 5.3.6 con Driver PDO per MySQL, versione 5.1.61 della libreria client. Tutto è compilato a mano.PDO bindValue con PDO :: PARAM_BOOL causa l'esecuzione dell'istruzione in modo non riuscito

Quando eseguo il bind dei parametri con bindValue e imposta il terzo parametro come \ PDO :: PARAM_BOOL, l'istruzione execute restituisce false e non succede nulla (nessun dato inserito in MySQL, nemmeno nessuna eccezione). Quando non uso il terzo parametro, va bene. In realtà io non posso ommit terzo parametro, bacues Doctrine2 DBAL imposta, mentre i parametri di conversione ...

Ecco il codice:

<?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())); 

Risultato:

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

Che forse potrebbe andare storto? Ho provato su altri 4 server e non ho avuto questo bug. Anche se passo '0' (come una stringa) a $stmt->bindValue(5, false, \PDO::PARAM_BOOL); funziona bene.

+0

OK, il problema è risolto in PHP 5.3.10 su questo server (Red Hat 5.4) –

risposta

12

Ho avuto lo stesso problema su Ubuntu con PHP 5.3.10. (È interessante notare che non c'era nessun problema sulle finestre con WAMP ...)

In realtà si tratta di un bug noto in PDO: https://bugs.php.net/bug.php?id=38546

Io uso DOP :: PARAM_INT invece di PDO :: PARAM_BOOL. Funziona bene e non devi convertire i booleani in stringhe come sopra.