Descrizione

Elenco dei parametri

Controls the contents of the returned array as documented in
PDOStatement::fetch().
Defaults to value of PDO::ATTR_DEFAULT_FETCH_MODE
(which defaults to PDO::FETCH_BOTH)

To return an array consisting of all values of a single column from
the result set, specify PDO::FETCH_COLUMN. You
can specify which column you want with the
column-index parameter.

To fetch only the unique values of a single column from the result set,
bitwise-OR PDO::FETCH_COLUMN with
PDO::FETCH_UNIQUE.

To return an associative array grouped by the values of a specified
column, bitwise-OR PDO::FETCH_COLUMN with
PDO::FETCH_GROUP.

fetch_argument

This argument has a different meaning depending on the value of
the fetch_style parameter:

PDO::FETCH_COLUMN: Returns the indicated 0-indexed
column.

PDO::FETCH_CLASS: Returns instances of the specified
class, mapping the columns of each row to named properties in the class.

PDO::FETCH_FUNC: Returns the results of calling the
specified function, using each row's columns as parameters in the call.

ctor_args

Arguments of custom class constructor when the fetch_style
parameter is PDO::FETCH_CLASS.

Valori restituiti

PDOStatement::fetchAll() returns an array containing
all of the remaining rows in the result set. The array represents each
row as either an array of column values or an object with properties
corresponding to each column name. An empty array is returned if there
are zero results to fetch, or FALSE on failure.

Using this method to fetch large result sets will result in a heavy
demand on system and possibly network resources. Rather than retrieving
all of the data and manipulating it in PHP, consider using the database
server to manipulate the result sets. For example, use the WHERE and
ORDER BY clauses in SQL to restrict results before retrieving and
processing them with PHP.

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.

User Contributed Notes 25 notes

You might find yourself wanting to use FETCH_GROUP and FETCH_ASSOC at the same time, to get your table's primary key as the array key:<?php// $stmt is some query like "SELECT rowid, username, comment"$results = $stmt->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC);

Getting foreach to play nicely with some data from PDO FetchAll()I was not understanding to use the $value part of the foreach properly, I hope this helps someone else.Example:<?php $stmt = $this->db->prepare('SELECT title, FMarticle_id FROM articles WHERE domain_name =:domain_name');$stmt->bindValue(':domain_name', $domain);$stmt->execute();$article_list = $stmt->fetchAll(PDO::FETCH_ASSOC);?>which gives:

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.

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.

PLEASE BE AWARE: If you do an OUTER LEFT JOIN and set PDO FetchALL to PDO::FETCH_ASSOC, any primary key you used in the OUTER LEFT JOIN will be set to a blank if there are no records returned in the JOIN.

For example:<?php//query the product table and join to the image table and return any images, if we have any, for each product$sql = "SELECT * FROM product, imageLEFT OUTER JOIN image ON (product.product_id = image.product_id)";

The fix is to simply specify your field names in the SELECT clause instead of using the * as a wild card, or, you can also specify the field in addition to the *. The following example returns the product_id field correctly:

Error:SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.

Be careful when using PDO::FETCH_COLUMN with PDO::FETCH_GROUP. By default, results are grouped by first column (index 0) and second column (index 1) is returned. But, if you provide fetch argument, it wouldn't affect returned column, but grouping column. If grouping column is set explicitly , first columns is returned instead of second.

I was having trouble with returning PDO query results in arrays, due to the structure of the results of those queries (query array with arrays for results inside). Now when executing a fetchAll() on a query you get the following:

This is a smaller array, but return results in a HUGE database this way and it quickly becomes IMPPOSIBRU to read out the array given.

This is where the glorious foreach comes in! We all know the basic writing of a foreach (foreach ($array as $key => $value) {...}), but with query results there's a twist, as both the $key AND $value variables are arrays, where $key is the array of the query containing the result arrays and $value is/are the result array(s) themselves.

So in this foreach we have to talk directly to $value to get our results! This would look something like this:

<?php

foreach ($query as $key => $value) { return $value["key2"]; }

?>

This already does the job, but to return them in arrays we have to change our return code a little bit, like so:

This foreach loops through the query array, just like the firs one, but the return code just doesn't return the keys representing the result arrays ($key) and returns only keys from inside the result arrays, stored in $value. This gives us an array looking like this:

Array ( [key1] => "value1", [key2] => "value2")

This should return all the values in your array, if written properly. I'm still having problems with the foreach not returning ALL results, but this note was mainly to show how it can be returned in an array.

Something missing in the doc.If for instance you try to fetchAll(PDO::CLASS, "Class") it sometimes return an array of objects with NULL values, but the count of objects fetched correspond to table rows.

In this way works fine:fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, "Class");

You might find yourself wanting to use FETCH_GROUP and FETCH_ASSOC at the same time, to get your table's primary key as the array key - use FETCH_UNIQUE also, or you will probably get some unintended behaivor:<?php// $stmt is some query like "SELECT rowid, username, comment"