php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #68783 pgsql: PDO::fetchColumn() false returned for value false and no rows returned
Submitted: 2015-01-09 21:01 UTC Modified: 2015-02-04 02:34 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: jon dot dufresne at gmail dot com Assigned: aharvey (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.5.20 OS: Linux
Private report: No CVE-ID: None
 [2015-01-09 21:01 UTC] 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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-02-03 10:01 UTC] mbeccati@php.net
-Type: Bug +Type: Documentation Problem
 [2015-02-03 10:01 UTC] mbeccati@php.net
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.
 [2015-02-04 02:34 UTC] aharvey@php.net
Automatic comment from SVN on behalf of aharvey
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=335883
Log: Update PDOStatement::fetchColumn() docs for the boolean column issue.

Fixes doc bug #68783 (pgsql: PDO::fetchColumn() false returned for value false
and no rows returned).
 [2015-02-04 02:34 UTC] aharvey@php.net
-Status: Open +Status: Closed -Package: PDO PgSQL +Package: Documentation problem -Assigned To: +Assigned To: aharvey
 [2015-02-04 02:34 UTC] aharvey@php.net
The fix for this bug has been committed.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.


 [2015-02-04 02:34 UTC] aharvey@php.net
Bah, wrong message, but the manual's updated.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 12:01:29 2024 UTC