<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >PDOStatement->fetch()</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->execute()" HREF="function.pdostatement-execute.html"><LINK REL="NEXT" TITLE="PDOStatement->fetchAll()" HREF="function.pdostatement-fetchall.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-execute.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.pdostatement-fetchall.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.PDOStatement-fetch" ></A >PDOStatement->fetch()</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN177589" ></A ><P > (no version information, might be only in CVS)</P >PDOStatement->fetch() -- Fetches the next row from a result set </DIV ><DIV CLASS="refsect1" ><A NAME="AEN177592" ></A ><H2 >说明</H2 >class <B CLASS="classname" >PDOStatement</B > { <BR ></BR >mixed <B CLASS="methodname" >fetch</B > ( [int fetch_style [, int cursor_orientation [, int cursor_offset]]] )<BR ></BR >}<P > Fetches a row from a result set associated with a PDOStatement object. The <CODE CLASS="parameter" >fetch_style</CODE > parameter determines how PDO returns the row. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN177611" ></A ><H2 >参数</H2 ><P > <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><CODE CLASS="parameter" >fetch_style</CODE ></DT ><DD ><P > Controls how the next row will be returned to the caller. This value must be one of the <TT CLASS="literal" >PDO::FETCH_*</TT > constants, defaulting to <TT CLASS="literal" >PDO::FETCH_BOTH</TT >. <P ></P ><UL ><LI ><P > <TT CLASS="literal" >PDO::FETCH_ASSOC</TT >: returns an array indexed by column name as returned in your result set </P ></LI ><LI ><P > <TT CLASS="literal" >PDO::FETCH_BOTH</TT > (default): returns an array indexed by both column name and 0-indexed column number as returned in your result set </P ></LI ><LI ><P > <TT CLASS="literal" >PDO::FETCH_BOUND</TT >: returns <TT CLASS="constant" ><B >TRUE</B ></TT > and assigns the values of the columns in your result set to the PHP variables to which they were bound with the <A HREF="function.pdostatement-bindparam.html" >PDOStatement->bindParam()</A > method </P ></LI ><LI ><P > <TT CLASS="literal" >PDO::FETCH_CLASS</TT >: returns a new instance of the requested class, mapping the columns of the result set to named properties in the class. If <CODE CLASS="parameter" >fetch_style</CODE > includes PDO::FETCH_CLASSTYPE (e.g. <TT CLASS="literal" >PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE</TT >) then the name of the class is determined from a value of the first column. </P ></LI ><LI ><P > <TT CLASS="literal" >PDO::FETCH_INTO</TT >: updates an existing instance of the requested class, mapping the columns of the result set to named properties in the class </P ></LI ><LI ><P > <TT CLASS="literal" >PDO::FETCH_LAZY</TT >: combines <TT CLASS="literal" >PDO::FETCH_BOTH</TT > and <TT CLASS="literal" >PDO::FETCH_OBJ</TT >, creating the object variable names as they are accessed </P ></LI ><LI ><P > <TT CLASS="literal" >PDO::FETCH_NUM</TT >: returns an array indexed by column number as returned in your result set, starting at column 0 </P ></LI ><LI ><P > <TT CLASS="literal" >PDO::FETCH_OBJ</TT >: returns an anonymous object with property names that correspond to the column names returned in your result set </P ></LI ></UL > </P ></DD ><DT ><CODE CLASS="parameter" >cursor_orientation</CODE ></DT ><DD ><P > For a PDOStatement object representing a scrollable cursor, this value determines which row will be returned to the caller. This value must be one of the <TT CLASS="literal" >PDO::FETCH_ORI_*</TT > constants, defaulting to <TT CLASS="literal" >PDO::FETCH_ORI_NEXT</TT >. To request a scrollable cursor for your PDOStatement object, you must set the <TT CLASS="literal" >PDO::ATTR_CURSOR</TT > attribute to <TT CLASS="literal" >PDO::CURSOR_SCROLL</TT > when you prepare the SQL statement with <A HREF="function.pdo-prepare.html" >PDO->prepare()</A >. </P ></DD ><DT ><CODE CLASS="parameter" >offset</CODE ></DT ><DD ><P > For a PDOStatement object representing a scrollable cursor for which the <TT CLASS="literal" >cursor_orientation</TT > parameter is set to <TT CLASS="literal" >PDO::FETCH_ORI_ABS</TT >, this value specifies the absolute number of the row in the result set that shall be fetched. </P ><P > For a PDOStatement object representing a scrollable cursor for which the <TT CLASS="literal" >cursor_orientation</TT > parameter is set to <TT CLASS="literal" >PDO::FETCH_ORI_REL</TT >, this value specifies the row to fetch relative to the cursor position before <A HREF="function.pdostatement-fetch.html" >PDOStatement->fetch()</A > was called. </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN177674" ></A ><H2 >范例</H2 ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN177677" ></A ><P ><B >例 1. Fetching rows using different fetch styles</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">/* Exercise PDOStatement::fetch styles */<br /></font><font color="#007700">print(</font><font color="#DD0000">"PDO::FETCH_ASSOC: "</font><font color="#007700">);<br />print(</font><font color="#DD0000">"Return next row as an array indexed by column name\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">fetch</font><font color="#007700">(</font><font color="#0000BB">PDO</font><font color="#007700">::</font><font color="#0000BB">FETCH_ASSOC</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 />print(</font><font color="#DD0000">"\n"</font><font color="#007700">);<br /><br />print(</font><font color="#DD0000">"PDO::FETCH_BOTH: "</font><font color="#007700">);<br />print(</font><font color="#DD0000">"Return next row as an array indexed by both column name and number\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">fetch</font><font color="#007700">(</font><font color="#0000BB">PDO</font><font color="#007700">::</font><font color="#0000BB">FETCH_BOTH</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 />print(</font><font color="#DD0000">"\n"</font><font color="#007700">);<br /><br />print(</font><font color="#DD0000">"PDO::FETCH_LAZY: "</font><font color="#007700">);<br />print(</font><font color="#DD0000">"Return next row as an anonymous object with column names as properties\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">fetch</font><font color="#007700">(</font><font color="#0000BB">PDO</font><font color="#007700">::</font><font color="#0000BB">FETCH_LAZY</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 />print(</font><font color="#DD0000">"\n"</font><font color="#007700">);<br /><br />print(</font><font color="#DD0000">"PDO::FETCH_OBJ: "</font><font color="#007700">);<br />print(</font><font color="#DD0000">"Return next row as an anonymous object with column names as properties\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">fetch</font><font color="#007700">(</font><font color="#0000BB">PDO</font><font color="#007700">::</font><font color="#0000BB">FETCH_OBJ</font><font color="#007700">);<br />print </font><font color="#0000BB">$result</font><font color="#007700">-></font><font color="#0000BB">NAME</font><font color="#007700">;<br />print(</font><font color="#DD0000">"\n"</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" >PDO::FETCH_ASSOC: Return next row as an array indexed by column name Array ( [NAME] => apple [COLOUR] => red ) PDO::FETCH_BOTH: Return next row as an array indexed by both column name and number Array ( [NAME] => banana [0] => banana [COLOUR] => yellow [1] => yellow ) PDO::FETCH_LAZY: Return next row as an anonymous object with column names as properties PDORow Object ( [NAME] => orange [COLOUR] => orange ) PDO::FETCH_OBJ: Return next row as an anonymous object with column names as properties kiwi</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="AEN177682" ></A ><P ><B >例 2. Fetching rows with a scrollable cursor</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">function </font><font color="#0000BB">readDataForwards</font><font color="#007700">(</font><font color="#0000BB">$dbh</font><font color="#007700">) {<br /> </font><font color="#0000BB">$sql </font><font color="#007700">= </font><font color="#DD0000">'SELECT hand, won, bet FROM mynumbers ORDER BY BET'</font><font color="#007700">;<br /> </font><font color="#0000BB">try </font><font color="#007700">{<br /> </font><font color="#0000BB">$stmt </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="#0000BB">$sql</font><font color="#007700">, array(</font><font color="#0000BB">PDO</font><font color="#007700">::</font><font color="#0000BB">ATTR_CURSOR </font><font color="#007700">=> </font><font color="#0000BB">PDO</font><font color="#007700">::</font><font color="#0000BB">CURSOR_SCROLL</font><font color="#007700">));<br /> </font><font color="#0000BB">$stmt</font><font color="#007700">-></font><font color="#0000BB">execute</font><font color="#007700">();<br /> while (</font><font color="#0000BB">$row </font><font color="#007700">= </font><font color="#0000BB">$stmt</font><font color="#007700">-></font><font color="#0000BB">fetch</font><font color="#007700">(</font><font color="#0000BB">PDO</font><font color="#007700">::</font><font color="#0000BB">FETCH_NUM</font><font color="#007700">, </font><font color="#0000BB">PDO</font><font color="#007700">::</font><font color="#0000BB">FETCH_ORI_NEXT</font><font color="#007700">)) {<br /> </font><font color="#0000BB">$data </font><font color="#007700">= </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">] . </font><font color="#DD0000">"\t" </font><font color="#007700">. </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">] . </font><font color="#DD0000">"\t" </font><font color="#007700">. </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#0000BB">2</font><font color="#007700">] . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /> print </font><font color="#0000BB">$data</font><font color="#007700">;<br /> }<br /> </font><font color="#0000BB">$stmt </font><font color="#007700">= </font><font color="#0000BB">null</font><font color="#007700">;<br /> }<br /> </font><font color="#0000BB">catch </font><font color="#007700">(</font><font color="#0000BB">PDOException $e</font><font color="#007700">) {<br /> print </font><font color="#0000BB">$e</font><font color="#007700">-></font><font color="#0000BB">getMessage</font><font color="#007700">();<br /> }<br />}<br />function </font><font color="#0000BB">readDataBackwards</font><font color="#007700">(</font><font color="#0000BB">$dbh</font><font color="#007700">) {<br /> </font><font color="#0000BB">$sql </font><font color="#007700">= </font><font color="#DD0000">'SELECT hand, won, bet FROM mynumbers ORDER BY bet'</font><font color="#007700">;<br /> </font><font color="#0000BB">try </font><font color="#007700">{<br /> </font><font color="#0000BB">$stmt </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="#0000BB">$sql</font><font color="#007700">, array(</font><font color="#0000BB">PDO</font><font color="#007700">::</font><font color="#0000BB">ATTR_CURSOR </font><font color="#007700">=> </font><font color="#0000BB">PDO</font><font color="#007700">::</font><font color="#0000BB">CURSOR_SCROLL</font><font color="#007700">));<br /> </font><font color="#0000BB">$stmt</font><font color="#007700">-></font><font color="#0000BB">execute</font><font color="#007700">();<br /> </font><font color="#0000BB">$row </font><font color="#007700">= </font><font color="#0000BB">$stmt</font><font color="#007700">-></font><font color="#0000BB">fetch</font><font color="#007700">(</font><font color="#0000BB">PDO</font><font color="#007700">::</font><font color="#0000BB">FETCH_NUM</font><font color="#007700">, </font><font color="#0000BB">PDO</font><font color="#007700">::</font><font color="#0000BB">FETCH_ORI_LAST</font><font color="#007700">);<br /> do {<br /> </font><font color="#0000BB">$data </font><font color="#007700">= </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">] . </font><font color="#DD0000">"\t" </font><font color="#007700">. </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">] . </font><font color="#DD0000">"\t" </font><font color="#007700">. </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#0000BB">2</font><font color="#007700">] . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /> print </font><font color="#0000BB">$data</font><font color="#007700">;<br /> } while (</font><font color="#0000BB">$row </font><font color="#007700">= </font><font color="#0000BB">$stmt</font><font color="#007700">-></font><font color="#0000BB">fetch</font><font color="#007700">(</font><font color="#0000BB">PDO</font><font color="#007700">::</font><font color="#0000BB">FETCH_NUM</font><font color="#007700">, </font><font color="#0000BB">PDO</font><font color="#007700">::</font><font color="#0000BB">FETCH_ORI_PRIOR</font><font color="#007700">));<br /> </font><font color="#0000BB">$stmt </font><font color="#007700">= </font><font color="#0000BB">null</font><font color="#007700">;<br /> }<br /> </font><font color="#0000BB">catch </font><font color="#007700">(</font><font color="#0000BB">PDOException $e</font><font color="#007700">) {<br /> print </font><font color="#0000BB">$e</font><font color="#007700">-></font><font color="#0000BB">getMessage</font><font color="#007700">();<br /> }<br />}<br /><br />print </font><font color="#DD0000">"Reading forwards:\n"</font><font color="#007700">;<br /></font><font color="#0000BB">readDataForwards</font><font color="#007700">(</font><font color="#0000BB">$conn</font><font color="#007700">);<br /><br />print </font><font color="#DD0000">"Reading backwards:\n"</font><font color="#007700">;<br /></font><font color="#0000BB">readDataBackwards</font><font color="#007700">(</font><font color="#0000BB">$conn</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" >Reading forwards: 21 10 5 16 0 5 19 20 10 Reading backwards: 19 20 10 16 0 5 21 10 5</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN177687" ></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-fetchall.html" >PDOStatement->fetchAll()</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-execute.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-fetchall.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >PDOStatement->execute()</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->fetchAll()</TD ></TR ></TABLE ></DIV ></BODY ></HTML >