Doc #66542 [NEW]: Example and explanation do not match
| From: | kander at gmail dot com | Date: | Wed, 22 Jan 2014 11:04:51 +0000 |
| Subject: | Doc #66542 [NEW]: Example and explanation do not match | ||
| Groups: | php.doc.bugs | ||
| Request: | Send a blank email to doc-bugs+get-10866@lists.php.net to get a copy of this message | ||
From: kander at gmail dot com
Operating system:
PHP version: Irrelevant
Package: PDO Core
Bug Type: Documentation Problem
Bug description:Example and explanation do not match
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 bug report at https://bugs.php.net/bug.php?id=66542&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=66542&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=66542&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=66542&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=66542&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=66542&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=66542&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=66542&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=66542&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=66542&r=support
Expected behavior: https://bugs.php.net/fix.php?id=66542&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=66542&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=66542&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=66542&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=66542&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=66542&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=66542&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=66542&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=66542&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=66542&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=66542&r=mysqlcfg