php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #66542 Example and explanation do not match
Submitted: 2014-01-22 11:04 UTC Modified: 2017-10-24 08:31 UTC
From: kander at gmail dot com Assigned:
Status: Open Package: PDO related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2014-01-22 11:04 UTC] 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"
  }
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-11-03 08:24 UTC] mbeccati@php.net
The third example uses the data that is visible in the previous ones, except watermelons changed from pink to green.
 [2017-03-16 12:16 UTC] 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
 [2017-10-24 08:31 UTC] kalle@php.net
-Package: PDO Core +Package: PDO related
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Wed Oct 17 22:01:26 2018 UTC