Bug #69856 [Opn]: bindColumn() casts NULL to zero if PDO::PARAM_INT is used

From: Date: Fri, 26 Jun 2015 00:12:03 +0000
Subject: Bug #69856 [Opn]: bindColumn() casts NULL to zero if PDO::PARAM_INT is used
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-193896@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=69856&edit=1 ID: 69856 User updated by: abcaeffchen at gmail dot com Reported by: abcaeffchen at gmail dot com -Summary: bindColumn() casts null zero if PDO::PARAM_INT is used +Summary: bindColumn() casts NULL to zero if PDO::PARAM_INT is used Status: Open Type: Bug Package: PDO MySQL Operating System: Windows PHP Version: 5.6.10 Block user comment: N Private report: N New Comment: Corrected the summary Previous Comments: ------------------------------------------------------------------------ [2015-06-17 03:07:13] abcaeffchen at gmail dot com Description: ------------ PDO fetches fields to strings by default. If the field contains NULL in database the returned value will also be null. But if using PDOStatement::bindColumn with PDO::PARAM_INT, all null values are casted to zero (as integer), so from the PHP side one cannot tell if the database field contains zero or null. The same happens with PDO::PARAM_BOOL. Here null is casted to false. There is a workaround to this: After turning off emulated prepared statements the returned values have the expected types. But this results in bug #69257. Test script: --------------- $db = new PDO('mysql:host=127.0.0.1;dbname=my_db;charset=UTF8',$user,$pass); // Let field contain NULL in all (matching) rows $stmt = $db->prepare('SELECT field FROM table WHERE other_field = ?'); $stmt->execute([1]); $stmt->bindColumn(1,$field,PDO::PARAM_INT); $stmt->fetch(PDO::FETCH_BOUND); var_dump($field); // 0 (integer) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=69856&edit=1

« previous php.bugs (#193896) next »