<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >PDOStatement->fetchAll()</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="PDO Functions" HREF="ref.pdo.html"><LINK REL="PREVIOUS" TITLE="PDOStatement->fetch()" HREF="function.pdostatement-fetch.html"><LINK REL="NEXT" TITLE="PDOStatement->fetchColumn()" HREF="function.pdostatement-fetchcolumn.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.pdostatement-fetch.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.pdostatement-fetchcolumn.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.PDOStatement-fetchAll" ></A >PDOStatement->fetchAll()</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN177702" ></A ><P > (no version information, might be only in CVS)</P >PDOStatement->fetchAll() -- Returns an array containing all of the result set rows </DIV ><DIV CLASS="refsect1" ><A NAME="AEN177705" ></A ><H2 >说明</H2 >class <B CLASS="classname" >PDOStatement</B > { <BR ></BR >array <B CLASS="methodname" >fetchAll</B > ( [int fetch_style [, int column_index]] )<BR ></BR >}</DIV ><DIV CLASS="refsect1" ><A NAME="AEN177719" ></A ><H2 >参数</H2 ><P > <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><CODE CLASS="parameter" >fetch_style</CODE ></DT ><DD ><P > Controls the contents of the returned array as documented in <A HREF="function.pdostatement-fetch.html" >PDOStatement->fetch()</A >. Defaults to <TT CLASS="literal" >PDO::FETCH_BOTH</TT >. </P ><P > To return an array consisting of all values of a single column from the result set, specify <TT CLASS="literal" >PDO::FETCH_COLUMN</TT >. You can specify which column you want with the <CODE CLASS="parameter" >column-index</CODE > parameter. </P ><P > To fetch only the unique values of a single column from the result set, bitwise-OR <TT CLASS="literal" >PDO::FETCH_COLUMN</TT > with <TT CLASS="literal" >PDO::FETCH_UNIQUE</TT >. </P ><P > To return an associative array grouped by the values of a specified column, bitwise-OR <TT CLASS="literal" >PDO::FETCH_COLUMN</TT > with <TT CLASS="literal" >PDO::FETCH_GROUP</TT >. </P ></DD ><DT ><CODE CLASS="parameter" >column_index</CODE ></DT ><DD ><P > Returns the indicated 0-indexed column when the value of <CODE CLASS="parameter" >fetch_style</CODE > is <TT CLASS="literal" >PDO::FETCH_COLUMN</TT >. Defaults to <TT CLASS="literal" >0</TT >. </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN177747" ></A ><H2 >返回值</H2 ><P > <A HREF="function.pdostatement-fetchall.html" >PDOStatement->fetchAll()</A > 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. </P ><P > 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 SORT BY clauses in SQL to restrict results before retrieving and processing them with PHP. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN177752" ></A ><H2 >范例</H2 ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN177755" ></A ><P ><B >例 1. Fetch all remaining rows in a result set</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$sth </font><font color="#007700">= </font><font color="#0000BB">$dbh</font><font color="#007700">-></font><font color="#0000BB">prepare</font><font color="#007700">(</font><font color="#DD0000">"SELECT name, colour FROM fruit"</font><font color="#007700">);<br /></font><font color="#0000BB">$sth</font><font color="#007700">-></font><font color="#0000BB">execute</font><font color="#007700">();<br /><br /></font><font color="#FF8000">/* Fetch all of the remaining rows in the result set */<br /></font><font color="#007700">print(</font><font color="#DD0000">"Fetch all of the remaining rows in the result set:\n"</font><font color="#007700">);<br /></font><font color="#0000BB">$result </font><font color="#007700">= </font><font color="#0000BB">$sth</font><font color="#007700">-></font><font color="#0000BB">fetchAll</font><font color="#007700">();<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$result</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >上例将输出:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Fetch all of the remaining rows in the result set: Array ( [0] => Array ( [NAME] => pear [0] => pear [COLOUR] => green [1] => green ) [1] => Array ( [NAME] => watermelon [0] => watermelon [COLOUR] => pink [1] => pink ) )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN177760" ></A ><P ><B >例 2. Fetching all values of a single column from a result set</B ></P ><P > The following example demonstrates how to return all of the values of a single column from a result set, even though the SQL statement itself may return multiple columns per row. </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$sth </font><font color="#007700">= </font><font color="#0000BB">$dbh</font><font color="#007700">-></font><font color="#0000BB">prepare</font><font color="#007700">(</font><font color="#DD0000">"SELECT name, colour FROM fruit"</font><font color="#007700">);<br /></font><font color="#0000BB">$sth</font><font color="#007700">-></font><font color="#0000BB">execute</font><font color="#007700">();<br /><br /></font><font color="#FF8000">/* Fetch all of the values of the first column */<br /></font><font color="#0000BB">$result </font><font color="#007700">= </font><font color="#0000BB">$sth</font><font color="#007700">-></font><font color="#0000BB">fetchAll</font><font color="#007700">(</font><font color="#0000BB">PDO</font><font color="#007700">::</font><font color="#0000BB">FETCH_COLUMN</font><font color="#007700">, </font><font color="#0000BB">0</font><font color="#007700">);<br /></font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">$result</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >上例将输出:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array(3) ( [0] => string(5) => apple [1] => string(4) => pear [2] => string(10) => watermelon )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN177766" ></A ><P ><B >例 3. Grouping all values by a single column</B ></P ><P > 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 <TT CLASS="literal" >apple</TT > and <TT CLASS="literal" >pear</TT > are returned as arrays that contain two different colours, while <TT CLASS="literal" >watermelon</TT > is returned as an array that contains only one colour. </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$insert </font><font color="#007700">= </font><font color="#0000BB">$dbh</font><font color="#007700">-></font><font color="#0000BB">prepare</font><font color="#007700">(</font><font color="#DD0000">"INSERT INTO fruit(name, colour) VALUES (?, ?)"</font><font color="#007700">);<br /></font><font color="#0000BB">$insert</font><font color="#007700">-></font><font color="#0000BB">execute</font><font color="#007700">(</font><font color="#DD0000">'apple'</font><font color="#007700">, </font><font color="#DD0000">'green'</font><font color="#007700">);<br /></font><font color="#0000BB">$insert</font><font color="#007700">-></font><font color="#0000BB">execute</font><font color="#007700">(</font><font color="#DD0000">'pear'</font><font color="#007700">, </font><font color="#DD0000">'yellow'</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$sth </font><font color="#007700">= </font><font color="#0000BB">$dbh</font><font color="#007700">-></font><font color="#0000BB">prepare</font><font color="#007700">(</font><font color="#DD0000">"SELECT name, colour FROM fruit"</font><font color="#007700">);<br /></font><font color="#0000BB">$sth</font><font color="#007700">-></font><font color="#0000BB">execute</font><font color="#007700">();<br /><br /></font><font color="#FF8000">/* Group values by the first column */<br /></font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">$sth</font><font color="#007700">-></font><font color="#0000BB">fetchAll</font><font color="#007700">(</font><font color="#0000BB">PDO</font><font color="#007700">::</font><font color="#0000BB">FETCH_COLUMN</font><font color="#007700">|</font><font color="#0000BB">PDO</font><font color="#007700">::</font><font color="#0000BB">FETCH_GROUP</font><font color="#007700">));<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >上例将输出:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >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" } }</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN177775" ></A ><H2 >参见</H2 ><P > <P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD ><A HREF="function.pdo-query.html" >PDO->query()</A ></TD ></TR ><TR ><TD ><A HREF="function.pdostatement-fetch.html" >PDOStatement->fetch()</A ></TD ></TR ><TR ><TD ><A HREF="function.pdostatement-fetchcolumn.html" >PDOStatement->fetchColumn()</A ></TD ></TR ><TR ><TD ><A HREF="function.pdo-prepare.html" >PDO->prepare()</A ></TD ></TR ><TR ><TD ><A HREF="function.pdostatement-setfetchmode.html" >PDOStatement->setFetchMode()</A ></TD ></TR ></TBODY ></TABLE ><P ></P > </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.pdostatement-fetch.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.pdostatement-fetchcolumn.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >PDOStatement->fetch()</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.pdo.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >PDOStatement->fetchColumn()</TD ></TR ></TABLE ></DIV ></BODY ></HTML >