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

From: Date: Sun, 10 Jul 2016 14:51:59 +0000
Subject: Req #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-202175@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 to zero if PDO::PARAM_INT is used Status: Open Type: Feature/Change Request Package: PDO related Operating System: Windows PHP Version: 5.6.10 Block user comment: N Private report: N New Comment: If you turn off emulated prepared statements, the behavior is as expected. So I still think that this is a bug in emulating the prepared statement. At least it is inconsistent. Also notice, that with PDO::PARAM_STR can NULL be fetched from the database. Previous Comments: ------------------------------------------------------------------------ [2016-06-30 11:11:44] cmb@php.net In my opinion, this behavior is not a bug. You ask for an int, and you get one; same as (int) null === 0. Of course, the desired behavior (getting null or an int) would be useful, but other constants should be introduced for this (maybe PARAM_NULLABLE_INT or PARAM_NULL_OR_INT), if only for backward compatibility. Anyhow, changing to feature request. ------------------------------------------------------------------------ [2015-06-26 00:12:03] abcaeffchen at gmail dot com Corrected the summary ------------------------------------------------------------------------ [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 (#202175) next »