Doc #68783 [Csd]: pgsql: PDO::fetchColumn() false returned for value false and no rows returned

From: Date: Wed, 04 Feb 2015 02:34:38 +0000
Subject: Doc #68783 [Csd]: 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-11922@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: aharvey@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: Closed Type: Documentation Problem Package: Documentation problem Operating System: Linux PHP Version: 5.5.20 Assigned To: aharvey Block user comment: N Private report: N New Comment: Bah, wrong message, but the manual's updated. Previous Comments: ------------------------------------------------------------------------ [2015-02-04 02:34:19] 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:13] 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-03 10:01:10] 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-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

« previous php.doc.bugs (#11922) next »