<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="pdo.query" xmlns="http://docbook.org/ns/docbook">
 <refnamediv>
  <refname>PDO::query</refname>
  <refpurpose>
   Prepares and executes an SQL statement without placeholders
  </refpurpose>
 </refnamediv>
 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis role="PDO">
   <modifier>public</modifier> <type class="union"><type>PDOStatement</type><type>false</type></type><methodname>PDO::query</methodname>
   <methodparam><type>string</type><parameter>query</parameter></methodparam>
   <methodparam choice="opt"><type class="union"><type>int</type><type>null</type></type><parameter>fetchMode</parameter><initializer>&null;</initializer></methodparam>
  </methodsynopsis>

  <methodsynopsis role="PDO">
   <modifier>public</modifier> <type class="union"><type>PDOStatement</type><type>false</type></type><methodname>PDO::query</methodname>
   <methodparam><type>string</type><parameter>query</parameter></methodparam>
   <methodparam choice="opt"><type class="union"><type>int</type><type>null</type></type><parameter>fetchMode</parameter><initializer>PDO::FETCH_COLUMN</initializer></methodparam>
   <methodparam><type>int</type><parameter>colno</parameter></methodparam>
  </methodsynopsis>

  <methodsynopsis role="PDO">
   <modifier>public</modifier> <type class="union"><type>PDOStatement</type><type>false</type></type><methodname>PDO::query</methodname>
   <methodparam><type>string</type><parameter>query</parameter></methodparam>
   <methodparam choice="opt"><type class="union"><type>int</type><type>null</type></type><parameter>fetchMode</parameter><initializer>PDO::FETCH_CLASS</initializer></methodparam>
   <methodparam><type>string</type><parameter>classname</parameter></methodparam>
   <methodparam><type>array</type><parameter>constructorArgs</parameter></methodparam>
  </methodsynopsis>

  <methodsynopsis role="PDO">
   <modifier>public</modifier> <type class="union"><type>PDOStatement</type><type>false</type></type><methodname>PDO::query</methodname>
   <methodparam><type>string</type><parameter>query</parameter></methodparam>
   <methodparam choice="opt"><type class="union"><type>int</type><type>null</type></type><parameter>fetchMode</parameter><initializer>PDO::FETCH_INTO</initializer></methodparam>
   <methodparam><type>object</type><parameter>object</parameter></methodparam>
  </methodsynopsis>

  <para>
   <methodname>PDO::query</methodname> prepares and executes an SQL statement in
   a single function call, returning the statement as a <classname>PDOStatement</classname> object.
  </para>
  <para>
   For a query that you need to issue multiple times, you will realize better
   performance if you prepare a <classname>PDOStatement</classname> object using
   <methodname>PDO::prepare</methodname> and issue the statement with multiple
   calls to <methodname>PDOStatement::execute</methodname>.
  </para>
  <para>
   If you do not fetch all of the data in a result set before issuing your
   next call to <methodname>PDO::query</methodname>, your call may fail. Call
   <methodname>PDOStatement::closeCursor</methodname> to release the database
   resources associated with the <classname>PDOStatement</classname> object before issuing your
   next call to <methodname>PDO::query</methodname>.
  </para>

  <note>
   <para>
    If the <parameter>query</parameter> contains placeholders, the statement
    must be prepared and executed separately using <methodname>PDO::prepare</methodname> and 
    <methodname>PDOStatement::execute</methodname> methods.
   </para>
  </note>

 </refsect1>
 <refsect1 role="parameters">
  &reftitle.parameters;
  <para>
   <variablelist>
    <varlistentry>
     <term><parameter>query</parameter></term>
      <listitem>
       <para>
        The SQL statement to prepare and execute.
       </para>
       <para>
        If the SQL contains placeholders, <methodname>PDO::prepare</methodname> and 
        <methodname>PDOStatement::execute</methodname> must be used instead.
        Alternatively, the SQL can be prepared manually before calling 
        <methodname>PDO::query</methodname>, with the data properly formatted using
        <methodname>PDO::quote</methodname> if the driver supports it.
       </para>
      </listitem>
     </varlistentry>
    <varlistentry>
     <term><parameter>fetchMode</parameter></term>
      <listitem>
       <para>
        The default fetch mode for the returned <classname>PDOStatement</classname>.
        It must be one of the <link linkend="pdo.constants"><literal>PDO::FETCH_*</literal></link> constants.
       </para>
       <para>
        If this argument is passed to the function, the remaining arguments
        will be treated as though <methodname>PDOStatement::setFetchMode</methodname>
        was called on the resultant statement object.
        The subsequent arguments vary depending on the selected fetch mode.
       </para>
      </listitem>
     </varlistentry>
   </variablelist>
  </para>
 </refsect1>
 <refsect1 role="returnvalues">
  &reftitle.returnvalues;
  <para>
    Returns a <classname>PDOStatement</classname> object&return.falseforfailure;.
  </para>
 </refsect1>

 <refsect1 role="errors">
  &reftitle.errors;
  &pdo.errors;
 </refsect1>

 <refsect1 role="examples">
  &reftitle.examples;
  <para>
   <example>
    <title>SQL with no placeholders can be executed using <methodname>PDO::query</methodname></title>
    <programlisting role="php">
<![CDATA[
<?php
$sql = 'SELECT name, color, calories FROM fruit ORDER BY name';
foreach ($conn->query($sql) as $row) {
    print $row['name'] . "\t";
    print $row['color'] . "\t";
    print $row['calories'] . "\n";
}
?>
]]>
    </programlisting>
    &example.outputs;
    <screen>
<![CDATA[
apple   red     150
banana  yellow  250
kiwi    brown   75
lemon   yellow  25
orange  orange  300
pear    green   150
watermelon      pink    90
]]>
    </screen>
   </example>
  </para>
 </refsect1>


 <refsect1 role="seealso">
  &reftitle.seealso;
  <para>
   <simplelist>
    <member><methodname>PDO::exec</methodname></member>
    <member><methodname>PDO::prepare</methodname></member>
    <member><methodname>PDOStatement::execute</methodname></member>
   </simplelist>
  </para>
 </refsect1>


</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
