Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > by-pkgid > 05cd670d8a02b2b4a0ffb1756f2e8308 > files > 8935

php-manual-zh-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
>PDO-&#62;query()</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="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"
>PHP 手册</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.pdo-prepare.html"
ACCESSKEY="P"
>上一页</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"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.PDO-query"
></A
>PDO-&#62;query()</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN176828"
></A
><P
>    (no version information, might be only in CVS)</P
>PDO-&#62;query()&nbsp;--&nbsp;
   Executes an SQL statement, returning a result set as a PDOStatement object
  </DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN176831"
></A
><H2
>说明</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
>&#13;   <A
HREF="function.pdo-query.html"
>PDO-&#62;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
>&#13;   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-&#62;prepare()</A
> and issue the statement with multiple
   calls to <A
HREF="function.pdostatement-execute.html"
>PDOStatement-&#62;execute()</A
>.
  </P
><P
>&#13;   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-&#62;query()</A
>, your call may fail. Call
   <A
HREF="function.pdostatement-closecursor.html"
>PDOStatement-&#62;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-&#62;query()</A
>.
  </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </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-&#62;setFetchMode()</A
> on the resultant statement
    object.
   </P
></BLOCKQUOTE
></DIV
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN176902"
></A
><H2
>参数</H2
><P
>&#13;   <P
></P
><DIV
CLASS="variablelist"
><DL
><DT
><CODE
CLASS="parameter"
>statement</CODE
></DT
><DD
><P
>&#13;        The SQL statement to prepare and execute.
       </P
></DD
></DL
></DIV
>
  </P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN176911"
></A
><H2
>返回值</H2
><P
>&#13;    <A
HREF="function.pdo-query.html"
>PDO-&#62;query()</A
> returns a PDOStatement object.
  </P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN176915"
></A
><H2
>范例</H2
><P
>&#13;   <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN176918"
></A
><P
><B
>例 1. Demonstrate PDO::query</B
></P
><P
>&#13;     A nice feature of <A
HREF="function.pdo-query.html"
>PDO-&#62;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">&lt;?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 />&nbsp;&nbsp;&nbsp;&nbsp;</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 />&nbsp;&nbsp;&nbsp;&nbsp;foreach (</font><font color="#0000BB">$conn</font><font color="#007700">-&gt;</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 />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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 />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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 />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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 />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>上例将输出:</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="AEN176925"
></A
><H2
>参见</H2
><P
>&#13;   <P
></P
><TABLE
BORDER="0"
><TBODY
><TR
><TD
><A
HREF="function.pdo-exec.html"
>PDO-&#62;exec()</A
></TD
></TR
><TR
><TD
><A
HREF="function.pdo-prepare.html"
>PDO-&#62;prepare()</A
></TD
></TR
><TR
><TD
><A
HREF="function.pdostatement-execute.html"
>PDOStatement-&#62;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"
>上一页</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.pdo-quote.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>PDO-&#62;prepare()</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ref.pdo.html"
ACCESSKEY="U"
>上一级</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>PDO-&#62;quote()</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>