note 116870 added to pdostatement.fetchall
| From: | akira at etnforum dot com | Date: | Fri, 13 Mar 2015 07:41:47 +0000 |
| Subject: | note 116870 added to pdostatement.fetchall | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-201932@lists.php.net to get a copy of this message | ||
There may be some user who needs to upgrade their MySQL class to PDO class. The way of fetching
results were changed from while loop into a foreach loop. For the people who wish to fetch the
results in a while loop, here is a simple trick.
<?php
$db = new DB();
$query = $db->prepare("SELECT * FROM CPUCategory");
$query = $db->execute();
$result = $db->fetchAll();
var_dump($result);
?>
The Output will be:
array(2) {
[0]=> array(2) {
["ccatid"]=> int(1)
["ccatname"]=> string(5) "Intel"
}
[1]=> array(2) {
["ccatid"]=> int(2)
["ccatname"]=> string(3) "AMD"
}
}
Never look like the output of old function.
[ORIGINAL STYLE] mysql_fetch_array($query)
[ MYSQL CLASS] $db->fetch_array($query)
And you may give up.
But there is a simple way to use while loop to fetch the results.
<?php
$db = new DB();
$query = $db->prepare("SELECT * FROM CPUCategory");
$query = $db->execute();
$result = $db->fetchAll();
$row = array_shift($result);
// If you need to fetch them now, put it in a while loop just like below:
// while($row = array_shift($result)) { ... }
var_dump($row);
?>
The Output will be in a single array with while loop returns TRUE:
array(2) {
["ccatid"]=> int(1)
["ccatname"]=> string(5) "Intel"
}
So after fetching this row, while loop runs again and fetch the next row until all row has fetched,
then the while loop will return false. (Just like the old function did)
When you need to upgrade to PDO class, not much code needs to be modified and remember.
----
Server IP: 72.52.91.14
Probable Submitter: 59.148.245.19
----
Manual Page -- http://php.net/manual/en/pdostatement.fetchall.php
Edit -- https://master.php.net/note/edit/116870
Del: integrated -- https://master.php.net/note/delete/116870/integrated
Del: useless -- https://master.php.net/note/delete/116870/useless
Del: bad code -- https://master.php.net/note/delete/116870/bad+code
Del: spam -- https://master.php.net/note/delete/116870/spam
Del: non-english -- https://master.php.net/note/delete/116870/non-english
Del: in docs -- https://master.php.net/note/delete/116870/in+docs
Del: other reasons-- https://master.php.net/note/delete/116870
Reject -- https://master.php.net/note/reject/116870
Search -- https://master.php.net/manage/user-notes.php