Doc #66542 [Opn]: Example and explanation do not match
| From: | mbeccati@php.net | Date: | Mon, 03 Nov 2014 08:24:41 +0000 |
| Subject: | Doc #66542 [Opn]: Example and explanation do not match | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-11601@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: mbeccati@php.net
Reported by: kander at gmail dot com
Summary: Example and explanation do not match
Status: Open
Type: Documentation Problem
Package: PDO Core
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
The third example uses the data that is visible in the previous ones, except watermelons changed
from pink to green.
Previous Comments:
------------------------------------------------------------------------
[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