Doc #66542 [Opn->Csd]: Example and explanation do not match

From: Date: Tue, 07 Apr 2020 11:43:12 +0000
Subject: Doc #66542 [Opn->Csd]: Example and explanation do not match
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-17466@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=66542&edit=1 ID: 66542 Updated by: cmb@php.net Reported by: kander at gmail dot com Summary: Example and explanation do not match -Status: Open +Status: Closed Type: Documentation Problem Package: PDO related PHP Version: Irrelevant -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: The documentation has obviously been fixed quite a while ago. Previous Comments: ------------------------------------------------------------------------ [2017-03-16 12:16:56] narf@php.net Automatic comment from SVN on behalf of narf Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=342140 Log: Docs bug #66542: Fix inconsistencies in PDOStatement::fetchAll() examples ------------------------------------------------------------------------ [2014-11-03 08:24:41] mbeccati@php.net The third example uses the data that is visible in the previous ones, except watermelons changed from pink to green. ------------------------------------------------------------------------ [2014-01-22 11:04:51] kander at gmail dot com Description: ------------ --- From manual page: http://www.php.net/pdostatement.fetchall --- The example code (cited below) does not mention watermelons in the input. The watermelon is part of previous examples, though - but not colored green - a running-on example does not seem to be the explanation. In this case, only apples and pears are mentioned in the input. The output however mentions watermelons, and the text following the example makes very specific claims about the presence of watermelons in the output. Of lesser importance is the addition of Red Apples. I have no qualms with apples in general, but this specific fruit/color combination is also missing from the input data. All in all, a very confusing example. Perhaps the fruit table contains data we're not aware of, which could be the result of a concurrency issue with another user also injecting fruity comestibles in the table? --- Example #3 Grouping all values by a single column The following example demonstrates how to return an associative array grouped by the values of the specified column in the result set. The array contains three keys: values apple and pear are returned as arrays that contain two different colours, while watermelon is returned as an array that contains only one colour. <?php $insert = $dbh->prepare("INSERT INTO fruit(name, colour) VALUES (?, ?)"); $insert->execute(array('apple', 'green')); $insert->execute(array('pear', 'yellow')); $sth = $dbh->prepare("SELECT name, colour FROM fruit"); $sth->execute(); /* Group values by the first column */ var_dump($sth->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP)); ?> The above example will output something similar to: array(3) { ["apple"]=> array(2) { [0]=> string(5) "green" [1]=> string(3) "red" } ["pear"]=> array(2) { [0]=> string(5) "green" [1]=> string(6) "yellow" } ["watermelon"]=> array(1) { [0]=> string(5) "green" } } Test script: --------------- $insert = $dbh->prepare("INSERT INTO fruit(name, colour) VALUES (?, ?)"); $insert->execute(array('apple', 'green')); $insert->execute(array('pear', 'yellow')); $sth = $dbh->prepare("SELECT name, colour FROM fruit"); $sth->execute(); /* Group values by the first column */ var_dump($sth->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP)); Expected result: ---------------- I expected there to be no watermelons, nor red apples in the var_dump output, based on the example code. When actually running the code, instead of blindly trusting the suggested output in the documentation, there are no watermelons (nor red apples) to be found. Why does the documentation say I should be on the lookout for these fruity snacks, then? array(3) { ["apple"]=> array(2) { [0]=> string(5) "green" [1]=> string(3) "red" } ["pear"]=> array(2) { [0]=> string(5) "green" [1]=> string(6) "yellow" } ["watermelon"]=> array(1) { [0]=> string(5) "green" } } Actual result: -------------- array(2) { ["apple"]=> array(1) { [0]=> string(5) "green" } ["pear"]=> array(1) { [0]=> string(6) "yellow" } } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=66542&edit=1

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