downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the SVN repository for this website on svn.php.net.

Source of: /manual/en/pdo.query.php

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once
dirname(__FILE__) ."/toc/class.pdo.inc";
$setup = array (
 
'home' =>
  array (
   
0 => 'index.php',
   
1 => 'PHP Manual',
  ),
 
'head' =>
  array (
   
0 => 'UTF-8',
   
1 => 'en',
  ),
 
'this' =>
  array (
   
0 => 'pdo.query.php',
   
1 => 'PDO::query',
  ),
 
'up' =>
  array (
   
0 => 'class.pdo.php',
   
1 => 'The PDO class',
  ),
 
'prev' =>
  array (
   
0 => 'pdo.prepare.php',
   
1 => 'PDO::prepare',
  ),
 
'next' =>
  array (
   
0 => 'pdo.quote.php',
   
1 => 'PDO::quote',
  ),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);

manual_header();
?>
<div id="pdo.query" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PDO::query</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PECL pdo &gt;= 0.2.0)</p><p class="refpurpose"><span class="refname">PDO::query</span> &mdash; <span class="dc-title">
   Executes an SQL statement, returning a result set as a PDOStatement object
  </span></p>

 </div>
 <a name="pdo.query.description"></a><div class="refsect1 description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type"><a href="class.pdostatement.php" class="type PDOStatement">PDOStatement</a></span> <span class="methodname"><b>PDO::query</b></span>
    ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$statement</tt></span>
   )</div>


  <div class="methodsynopsis dc-description">
   <span class="type"><a href="class.pdostatement.php" class="type PDOStatement">PDOStatement</a></span> <span class="methodname"><b>PDO::query</b></span>
    ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$statement</tt></span>
   , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$PDO::FETCH_COLUMN</tt></span>
   , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$colno</tt></span>
   )</div>


  <div class="methodsynopsis dc-description">
   <span class="type"><a href="class.pdostatement.php" class="type PDOStatement">PDOStatement</a></span> <span class="methodname"><b>PDO::query</b></span>
    ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$statement</tt></span>
   , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$PDO::FETCH_CLASS</tt></span>
   , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$classname</tt></span>
   , <span class="methodparam"><span class="type">array</span> <tt class="parameter">$ctorargs</tt></span>
   )</div>


  <div class="methodsynopsis dc-description">
   <span class="type"><a href="class.pdostatement.php" class="type PDOStatement">PDOStatement</a></span> <span class="methodname"><b>PDO::query</b></span>
    ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$statement</tt></span>
   , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$PDO::FETCH_INTO</tt></span>
   , <span class="methodparam"><span class="type">object</span> <tt class="parameter">$object</tt></span>
   )</div>


  <p class="para rdfs-comment">
   <b>PDO::query()</b> 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 class="para">
   For a query that you need to issue multiple times, you will realize better
   performance if you prepare a PDOStatement object using
   <a href="pdo.prepare.php" class="function">PDO::prepare()</a> and issue the statement with multiple
   calls to <a href="pdostatement.execute.php" class="function">PDOStatement::execute()</a>.
  </p>
  <p class="para">
   If you do not fetch all of the data in a result set before issuing your
   next call to <b>PDO::query()</b>, your call may fail. Call
   <a href="pdostatement.closecursor.php" class="function">PDOStatement::closeCursor()</a> to release the database
   resources associated with the PDOStatement object before issuing your
   next call to <b>PDO::query()</b>.
  </p>

  <blockquote><p><b class="note">Note</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="pdostatement.setfetchmode.php" class="function">PDOStatement::setFetchMode()</a> on the resultant statement
    object.
   <br />
  </p></blockquote>

 </div>

 <a name="pdo.query.parameters"></a><div class="refsect1 parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   </p><dl>

    <dt class="varlistentry">

     <span class="term"><i><tt class="parameter">statement</tt></i>
</span>

      </dt><dd class="listitem">

       <p class="para">
        The SQL statement to prepare and execute.
       </p>
       <p class="para">
        Data inside the query should be <a href="pdo.quote.php" class="link">properly escaped</a>.
       </p>
      </dd>

    
   </dl>
<p>
  </p>
 </div>

 <a name="pdo.query.returnvalues"></a><div class="refsect1 returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
    <b>PDO::query()</b> returns a PDOStatement object, or <b><tt class="constant">FALSE</tt></b>
    on failure.
  </p>
 </div>


 <a name="pdo.query.examples"></a><div class="refsect1 examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   </p><div class="example">
    <p><b>Example #1 Demonstrate PDO::query</b></p>
    <div class="example-contents para"><p>
     A nice feature of <b>PDO::query()</b> is that it
     enables you to iterate over the rowset returned by a
     successfully executed SELECT statement.
    </p></div>
    <div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">getFruit</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$sql&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'SELECT&nbsp;name,&nbsp;colour,&nbsp;calories&nbsp;FROM&nbsp;fruit&nbsp;ORDER&nbsp;BY&nbsp;name'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #0000BB">$sql</span><span style="color: #007700">)&nbsp;as&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'NAME'</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\t"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'COLOUR'</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\t"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'CALORIES'</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents para"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
apple   red     150
banana  yellow  250
kiwi    brown   75
lemon   yellow  25
orange  orange  300
pear    green   150
watermelon      pink    90
</pre></div>
    </div>
   </div><p>
  </p>
 </div>



 <a name="pdo.query.seealso"></a><div class="refsect1 seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   </p><ul class="simplelist">
    <li class="member"><a href="pdo.exec.php" class="function" rel="rdfs-seeAlso">PDO::exec()</a> - Execute an SQL statement and return the number of affected rows</li>
    <li class="member"><a href="pdo.prepare.php" class="function" rel="rdfs-seeAlso">PDO::prepare()</a> - Prepares a statement for execution and returns a statement object</li>
    <li class="member"><a href="pdostatement.execute.php" class="function" rel="rdfs-seeAlso">PDOStatement::execute()</a> - Executes a prepared statement</li>
   </ul><p>
  </p>
 </div>



</div><?php manual_footer(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites