Bug->Doc #68783 [Opn]: pgsql: PDO::fetchColumn() false returned for value false and no rows returned
| From: | mbeccati@php.net | Date: | Tue, 03 Feb 2015 10:01:11 +0000 |
| Subject: | Bug->Doc #68783 [Opn]: pgsql: PDO::fetchColumn() false returned for value false and no rows returned | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-11916@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68783&edit=1
ID: 68783
Updated by: mbeccati@php.net
Reported by: jon dot dufresne at gmail dot com
Summary: pgsql: PDO::fetchColumn() false returned for value
false and no rows returned
Status: Open
-Type: Bug
+Type: Documentation Problem
Package: PDO PgSQL
Operating System: Linux
PHP Version: 5.5.20
Block user comment: N
Private report: N
New Comment:
Moving to documentation problem.
The documentation states that the return type is string, whereas it can be pretty much everything.
Even null wouldn't be appropriate as the result in case a row is not found.
The documentation should make it clear that it is not advisable to use the method to fetch a boolean
column (maybe this applies to other drivers too?) as it would be impossible to distinguish between
false and not-found. PDOStatment::fetch is better suited in that case.
Previous Comments:
------------------------------------------------------------------------
[2015-01-09 21:01:35] jon dot dufresne at gmail dot com
Description:
------------
PostgreSQL has proper support for boolean fields. Boolean database fields are returned to PHP as
boolean values. Additionally, when using fetchColumn() it is often useful to know if no rows were
returned. PDO reports this by returning the value false. There is a conflict here as false is now
overloaded to represent both "no rows returned" and "column value false". These
are two distinct cases that may need to be handled separately.
$ php --version
PHP 5.5.20 (cli) (built: Dec 18 2014 05:55:32)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies
with Xdebug v2.2.6, Copyright (c) 2002-2014, by Derick Rethans
The following script demonstrates this behavior.
Test script:
---------------
<?php
$db = new PDO('pgsql:dbname=test', 'postgres');
$db->exec('DROP TABLE IF EXISTS test_table');
$db->exec('CREATE TABLE test_table (field boolean)');
$stmt = $db->query('SELECT * FROM test_table');
var_dump($stmt->fetchColumn());
$db->exec('INSERT INTO test_table VALUES (FALSE)');
$stmt = $db->query('SELECT * FROM test_table');
var_dump($stmt->fetchColumn());
Expected result:
----------------
I expect the two var_dumps() to return different values, but they do not. (Alternatively, if an
exception was thrown that would also work, but PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION does
not make this happen.)
Actual result:
--------------
bool(false)
bool(false)
The two var_dump()s are identical making it difficult to know if no rows were returned or the value
false was returned.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68783&edit=1