Sophie

Sophie

distrib > Mandriva > 2008.1 > i586 > by-pkgid > 703d980c580707c382b4e43e25965bc5 > files > 3643

php-manual-pt_BR-5.2.4-1mdv2008.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>PDOStatement-&#62;fetch()</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="Manual do 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"
>Manual do PHP</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.pdostatement-execute.html"
ACCESSKEY="P"
>Anterior</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"
>Próxima</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.PDOStatement-fetch"
></A
>PDOStatement-&#62;fetch()</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN173163"
></A
><P
>    (no version information, might be only in CVS)</P
>PDOStatement-&#62;fetch()&nbsp;--&nbsp;
   Fetches the next row from a result set
  </DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN173166"
></A
><H2
>Descrição</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
>&#13;   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="AEN173185"
></A
><H2
>Parâmetros</H2
><P
>&#13;   <P
></P
><DIV
CLASS="variablelist"
><DL
><DT
><CODE
CLASS="parameter"
>fetch_style</CODE
></DT
><DD
><P
>&#13;       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
>&#13;         <TT
CLASS="literal"
>PDO::FETCH_ASSOC</TT
>: returns an array indexed by column
         name as returned in your result set
        </P
></LI
><LI
><P
>&#13;         <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
>&#13;         <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-&#62;bindParam()</A
>
         method
        </P
></LI
><LI
><P
>&#13;         <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
>&#13;         <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
>&#13;         <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
>&#13;         <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
>&#13;         <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
>&#13;       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-&#62;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
>&#13;       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-&#62;fetch()</A
> was called.
      </P
></DD
></DL
></DIV
>
  </P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN173248"
></A
><H2
>Exemplos</H2
><P
>&#13;   <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN173251"
></A
><P
><B
>Exemplo 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">&lt;?php<br />$sth </font><font color="#007700">= </font><font color="#0000BB">$dbh</font><font color="#007700">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</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">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>O exemplo acima irá imprimir:</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] =&#62; apple
    [COLOUR] =&#62; red
)

PDO::FETCH_BOTH: Return next row as an array indexed by both column name and number
Array
(
    [NAME] =&#62; banana
    [0] =&#62; banana
    [COLOUR] =&#62; yellow
    [1] =&#62; yellow
)

PDO::FETCH_LAZY: Return next row as an anonymous object with column names as properties
PDORow Object
(
    [NAME] =&#62; orange
    [COLOUR] =&#62; 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="AEN173256"
></A
><P
><B
>Exemplo 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">&lt;?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 />&nbsp;&nbsp;</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 />&nbsp;&nbsp;</font><font color="#0000BB">try </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$stmt </font><font color="#007700">= </font><font color="#0000BB">$dbh</font><font color="#007700">-&gt;</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">=&gt; </font><font color="#0000BB">PDO</font><font color="#007700">::</font><font color="#0000BB">CURSOR_SCROLL</font><font color="#007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$stmt</font><font color="#007700">-&gt;</font><font color="#0000BB">execute</font><font color="#007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;while (</font><font color="#0000BB">$row </font><font color="#007700">= </font><font color="#0000BB">$stmt</font><font color="#007700">-&gt;</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 />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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 />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print </font><font color="#0000BB">$data</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$stmt </font><font color="#007700">= </font><font color="#0000BB">null</font><font color="#007700">;<br />&nbsp;&nbsp;}<br />&nbsp;&nbsp;</font><font color="#0000BB">catch </font><font color="#007700">(</font><font color="#0000BB">PDOException $e</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;print </font><font color="#0000BB">$e</font><font color="#007700">-&gt;</font><font color="#0000BB">getMessage</font><font color="#007700">();<br />&nbsp;&nbsp;}<br />}<br />function </font><font color="#0000BB">readDataBackwards</font><font color="#007700">(</font><font color="#0000BB">$dbh</font><font color="#007700">) {<br />&nbsp;&nbsp;</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 />&nbsp;&nbsp;</font><font color="#0000BB">try </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$stmt </font><font color="#007700">= </font><font color="#0000BB">$dbh</font><font color="#007700">-&gt;</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">=&gt; </font><font color="#0000BB">PDO</font><font color="#007700">::</font><font color="#0000BB">CURSOR_SCROLL</font><font color="#007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$stmt</font><font color="#007700">-&gt;</font><font color="#0000BB">execute</font><font color="#007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$row </font><font color="#007700">= </font><font color="#0000BB">$stmt</font><font color="#007700">-&gt;</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 />&nbsp;&nbsp;&nbsp;&nbsp;do {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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 />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print </font><font color="#0000BB">$data</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;} while (</font><font color="#0000BB">$row </font><font color="#007700">= </font><font color="#0000BB">$stmt</font><font color="#007700">-&gt;</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 />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$stmt </font><font color="#007700">= </font><font color="#0000BB">null</font><font color="#007700">;<br />&nbsp;&nbsp;}<br />&nbsp;&nbsp;</font><font color="#0000BB">catch </font><font color="#007700">(</font><font color="#0000BB">PDOException $e</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;print </font><font color="#0000BB">$e</font><font color="#007700">-&gt;</font><font color="#0000BB">getMessage</font><font color="#007700">();<br />&nbsp;&nbsp;}<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">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>O exemplo acima irá imprimir:</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="AEN173261"
></A
><H2
>Veja também</H2
><P
>&#13;   <P
></P
><TABLE
BORDER="0"
><TBODY
><TR
><TD
><A
HREF="function.pdo-query.html"
>PDO-&#62;query()</A
></TD
></TR
><TR
><TD
><A
HREF="function.pdostatement-fetchall.html"
>PDOStatement-&#62;fetchAll()</A
></TD
></TR
><TR
><TD
><A
HREF="function.pdostatement-fetchcolumn.html"
>PDOStatement-&#62;fetchColumn()</A
></TD
></TR
><TR
><TD
><A
HREF="function.pdo-prepare.html"
>PDO-&#62;prepare()</A
></TD
></TR
><TR
><TD
><A
HREF="function.pdostatement-setfetchmode.html"
>PDOStatement-&#62;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"
>Anterior</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Principal</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.pdostatement-fetchall.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>PDOStatement-&#62;execute()</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ref.pdo.html"
ACCESSKEY="U"
>Acima</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>PDOStatement-&#62;fetchAll()</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>