Req->Bug #78419 [Opn->Nab]: Incorrect fetch return value

From: Date: Fri, 16 Aug 2019 07:11:28 +0000
Subject: Req->Bug #78419 [Opn->Nab]: Incorrect fetch return value
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-222263@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=78419&edit=1 ID: 78419 Updated by: requinix@php.net Reported by: ludwigdiehl at gmail dot com Summary: Incorrect fetch return value -Status: Open +Status: Not a bug -Type: Feature/Change Request +Type: Bug Package: PDO related Operating System: CentOS Linux release 7.6.1810 PHP Version: 7.3.8 Block user comment: N Private report: N New Comment: fetch() returns false because there are no rows to retrieve. No rows is not an error, but attempting to fetch rows when there are not any (more) could be considered one. It is not a significant error, and using the false value to identify the end of the results is convenient, so PHP will not produce a warning. Many other functions also use false to indicate an end of data, and changing this behavior will break BC for code that tests the return value ===false while offering no real gain. fetchAll() returns an empty array because there are no rows to retrieve. The resultset is empty so the array is empty. It is very common for queries to not return rows and having fetchAll() return false (or null) would be inconvenient in the many cases where the developer wants to foreach or count() the rows. Previous Comments: ------------------------------------------------------------------------ [2019-08-16 00:57:08] ludwigdiehl at gmail dot com Description: ------------ If you create a prepared statement from a non-results query, you get the following return values after calling fetchAll and fetch respectively: fetchAll: EMPTY ARRAY. Which is the desired return value. fetch: FALSE. Should it not be NULL? According to the documentation, "In all cases, FALSE is returned on failure but this is not an error isn't it?. Test script: --------------- $pdo = new PDO('mysql:host=x.x.x.x;dbname=xxx','user','password'); $stmt = $pdo->prepare('SELECT * FROM mytable WHERE 1=12'); $stmt->setFetchMode(PDO::FETCH_ASSOC); $stmt->execute(); $result = $stmt->fetch(); var_dump($result); $pdo = new PDO('mysql:host=x.x.x.x;dbname=xxx','user','password'); $stmt = $pdo->prepare('SELECT * FROM mytable WHERE 1=12'); $stmt->setFetchMode(PDO::FETCH_ASSOC); $stmt->execute(); $result = $stmt->fetchAll(); var_dump($result); Expected result: ---------------- I think NULL should be return value of the fetch method instead of FALSE for an empty result. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=78419&edit=1

« previous php.bugs (#222263) next »