<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >PDO->query()</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="PDO->prepare()" HREF="function.pdo-prepare.html"><LINK REL="NEXT" TITLE="PDO->quote()" HREF="function.pdo-quote.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.pdo-prepare.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.pdo-quote.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.PDO-query" ></A >PDO->query()</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN172402" ></A ><P > (no version information, might be only in CVS)</P >PDO->query() -- Executes an SQL statement, returning a result set as a PDOStatement object </DIV ><DIV CLASS="refsect1" ><A NAME="AEN172405" ></A ><H2 >Descrição</H2 >class <B CLASS="classname" >PDO</B > { <BR ></BR >PDOStatement <B CLASS="methodname" >query</B > ( string statement )<BR ></BR >}class <B CLASS="classname" >PDO</B > { <BR ></BR >bool <B CLASS="methodname" >query</B > ( string statement, int PDO::FETCH_COLUMN, int colno )<BR ></BR >}class <B CLASS="classname" >PDO</B > { <BR ></BR >bool <B CLASS="methodname" >query</B > ( string statement, int PDO::FETCH_CLASS, string classname, array ctorargs )<BR ></BR >}class <B CLASS="classname" >PDO</B > { <BR ></BR >bool <B CLASS="methodname" >query</B > ( string statement, int PDO::FETCH_INTO, object object )<BR ></BR >}<P > <A HREF="function.pdo-query.html" >PDO->query()</A > executes an SQL statement in a single function call, returning the result set (if any) returned by the statement as a PDOStatement object. </P ><P > For a query that you need to issue multiple times, you will realize better performance if you prepare a PDOStatement object using <A HREF="function.pdo-prepare.html" >PDO->prepare()</A > and issue the statement with multiple calls to <A HREF="function.pdostatement-execute.html" >PDOStatement->execute()</A >. </P ><P > If you do not fetch all of the data in a result set before issuing your next call to <A HREF="function.pdo-query.html" >PDO->query()</A >, your call may fail. Call <A HREF="function.pdostatement-closecursor.html" >PDOStatement->closeCursor()</A > to release the database resources associated with the PDOStatement object before issuing your next call to <A HREF="function.pdo-query.html" >PDO->query()</A >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Nota: </B > Although this function is only documented as having a single parameter, you may pass additional arguments to this function. They will be treated as though you called <A HREF="function.pdostatement-setfetchmode.html" >PDOStatement->setFetchMode()</A > on the resultant statement object. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN172476" ></A ><H2 >Parâmetros</H2 ><P > <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><CODE CLASS="parameter" >statement</CODE ></DT ><DD ><P > The SQL statement to prepare and execute. </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN172485" ></A ><H2 >Valores de retornado</H2 ><P > <A HREF="function.pdo-query.html" >PDO->query()</A > returns a PDOStatement object. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN172489" ></A ><H2 >Exemplos</H2 ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN172492" ></A ><P ><B >Exemplo 1. Demonstrate PDO::query</B ></P ><P > A nice feature of <A HREF="function.pdo-query.html" >PDO->query()</A > is that it enables you to iterate over the rowset returned by a successfully executed SELECT statement. </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">getFruit</font><font color="#007700">(</font><font color="#0000BB">$conn</font><font color="#007700">) {<br /> </font><font color="#0000BB">$sql </font><font color="#007700">= </font><font color="#DD0000">'SELECT name, colour, calories FROM fruit ORDER BY name'</font><font color="#007700">;<br /> foreach (</font><font color="#0000BB">$conn</font><font color="#007700">-></font><font color="#0000BB">query</font><font color="#007700">(</font><font color="#0000BB">$sql</font><font color="#007700">) as </font><font color="#0000BB">$row</font><font color="#007700">) {<br /> print </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#DD0000">'NAME'</font><font color="#007700">] . </font><font color="#DD0000">"\t"</font><font color="#007700">;<br /> print </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#DD0000">'COLOUR'</font><font color="#007700">] . </font><font color="#DD0000">"\t"</font><font color="#007700">;<br /> print </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#DD0000">'CALORIES'</font><font color="#007700">] . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /> }<br />}<br /></font><font color="#0000BB">?></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" >apple red 150 banana yellow 250 kiwi brown 75 lemon yellow 25 orange orange 300 pear green 150 watermelon pink 90</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN172499" ></A ><H2 >Veja também</H2 ><P > <P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD ><A HREF="function.pdo-exec.html" >PDO->exec()</A ></TD ></TR ><TR ><TD ><A HREF="function.pdo-prepare.html" >PDO->prepare()</A ></TD ></TR ><TR ><TD ><A HREF="function.pdostatement-execute.html" >PDOStatement->execute()</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.pdo-prepare.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.pdo-quote.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >PDO->prepare()</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" >PDO->quote()</TD ></TR ></TABLE ></DIV ></BODY ></HTML >