<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = 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.prepare.php',
    1 => 'PDO::prepare',
    2 => 'Prepares a statement for execution and returns a statement object',
  ),
  'up' => 
  array (
    0 => 'class.pdo.php',
    1 => 'PDO',
  ),
  'prev' => 
  array (
    0 => 'pdo.lastinsertid.php',
    1 => 'PDO::lastInsertId',
  ),
  'next' => 
  array (
    0 => 'pdo.query.php',
    1 => 'PDO::query',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/pdo/pdo/prepare.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="pdo.prepare" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PDO::prepare</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8, PHP 8,PECL pdo &gt;= 0.1.0)</p><p class="refpurpose"><span class="refname">PDO::prepare</span> &mdash; <span class="dc-title">
   Prepares a statement for execution and returns a statement object
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-pdo.prepare-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>PDO::prepare</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$query</code></span>, <span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$options</code><span class="initializer"> = []</span></span>): <span class="type"><span class="type"><a href="class.pdostatement.php" class="type PDOStatement">PDOStatement</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>


  <p class="para rdfs-comment">
   Prepares an SQL statement to be executed by the
   <span class="methodname"><a href="pdostatement.execute.php" class="methodname">PDOStatement::execute()</a></span> method. The statement template can
   contain zero or more named (:name) or question mark (?) parameter markers
   for which real values will be substituted when the statement is executed.
   Both named and question mark parameter markers cannot be used within the same
   statement template; only one or the other parameter style.
   Use these parameters to bind any user-input, do not include the user-input
   directly in the query.
  </p>
  <p class="para">
   You must include a unique parameter marker for each value you wish to pass
   in to the statement when you call <span class="methodname"><a href="pdostatement.execute.php" class="methodname">PDOStatement::execute()</a></span>.
   You cannot use a named parameter marker of the same name more than once in a prepared
   statement, unless emulation mode is on.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Parameter markers can represent a complete data literal only.
    Neither part of literal, nor keyword, nor identifier, nor whatever arbitrary query 
    part can be bound using parameters. For example, you cannot bind multiple values 
    to a single parameter in the IN() clause of an SQL statement.
   </p>
  </p></blockquote>
  <p class="para">
   Calling <span class="methodname"><strong>PDO::prepare()</strong></span> and
   <span class="methodname"><a href="pdostatement.execute.php" class="methodname">PDOStatement::execute()</a></span> for statements that will be
   issued multiple times with different parameter values optimizes the
   performance of your application by allowing the driver to negotiate
   client and/or server side caching of the query plan and meta information. Also, calling <span class="methodname"><strong>PDO::prepare()</strong></span> and
   <span class="methodname"><a href="pdostatement.execute.php" class="methodname">PDOStatement::execute()</a></span> helps to prevent SQL injection attacks by eliminating the need to
   manually quote and escape the parameters.
  </p>
  <p class="para">
   PDO will emulate prepared statements/bound parameters for drivers that do
   not natively support them, and can also rewrite named or question mark
   style parameter markers to something more appropriate, if the driver
   supports one style but not the other.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    The parser used for emulated prepared statements and for
    rewriting named or question mark style parameters supports the non standard
    backslash escapes for single- and double quotes. That means that terminating
    quotes immediately preceeded by a backslash are not recognized as such, which
    may result in wrong detection of parameters causing the prepared statement to
    fail when it is executed. A work-around is to not use emulated prepares for
    such SQL queries, and to avoid rewriting of parameters by using a parameter style
    which is natively supported by the driver.
   </span>
  </p></blockquote>
  <p class="para">
    As of PHP 7.4.0, question marks can be escaped by doubling them. That means that
    the <code class="literal">??</code> string will be translated to <code class="literal">?</code>
    when sending the query to the database.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-pdo.prepare-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">query</code></dt>
     <dd>
      <p class="para">
       This must be a valid SQL statement template for the target database server.
      </p>
     </dd>
    
    
     <dt><code class="parameter">options</code></dt>
     <dd>
      <p class="para">
       This array holds one or more key=&gt;value pairs to set
       attribute values for the PDOStatement object that this method
       returns. You would most commonly use this to set the
       <code class="literal">PDO::ATTR_CURSOR</code> value to
       <code class="literal">PDO::CURSOR_SCROLL</code> to request a scrollable cursor.
       Some drivers have driver-specific options that may be set at
       prepare-time.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-pdo.prepare-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   If the database server successfully prepares the statement,
   <span class="methodname"><strong>PDO::prepare()</strong></span> returns a
   <span class="classname"><a href="class.pdostatement.php" class="classname">PDOStatement</a></span> object.
   If the database server cannot successfully prepare the statement,
   <span class="methodname"><strong>PDO::prepare()</strong></span> returns <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> or emits
   <span class="classname"><a href="class.pdoexception.php" class="classname">PDOException</a></span> (depending on <a href="pdo.error-handling.php" class="link">error handling</a>).
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Emulated prepared statements does not communicate with the database server
    so <span class="methodname"><strong>PDO::prepare()</strong></span> does not check the statement.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 errors" id="refsect1-pdo.prepare-errors">
  <h3 class="title">Errors/Exceptions</h3>
  <p class="para">
Emits an error with level <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> if the attribute <strong><code><a href="pdo.constants.php#pdo.constants.attr-errmode">PDO::ATTR_ERRMODE</a></code></strong> is set
to <strong><code><a href="pdo.constants.php#pdo.constants.errmode-warning">PDO::ERRMODE_WARNING</a></code></strong>.
</p>
<p class="para">
Throws a <span class="classname"><a href="class.pdoexception.php" class="classname">PDOException</a></span> if the attribute <strong><code><a href="pdo.constants.php#pdo.constants.attr-errmode">PDO::ATTR_ERRMODE</a></code></strong>
is set to <strong><code><a href="pdo.constants.php#pdo.constants.errmode-exception">PDO::ERRMODE_EXCEPTION</a></code></strong>.
</p>
 </div>


 <div class="refsect1 examples" id="refsect1-pdo.prepare-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1"><p><strong>Example #1 SQL statement template with named parameters</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/* Execute a prepared statement by passing an array of values */<br /></span><span style="color: #0000BB">$sql </span><span style="color: #007700">= </span><span style="color: #DD0000">'SELECT name, colour, calories<br />    FROM fruit<br />    WHERE calories &lt; :calories AND colour = :colour'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$sth </span><span style="color: #007700">= </span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #0000BB">$sql</span><span style="color: #007700">, [</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">ATTR_CURSOR </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">CURSOR_FWDONLY</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">([</span><span style="color: #DD0000">'calories' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">150</span><span style="color: #007700">, </span><span style="color: #DD0000">'colour' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'red'</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$red </span><span style="color: #007700">= </span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">();<br /></span><span style="color: #FF8000">/* Array keys can be prefixed with colons ":" too (optional) */<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">([</span><span style="color: #DD0000">':calories' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">175</span><span style="color: #007700">, </span><span style="color: #DD0000">':colour' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'yellow'</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$yellow </span><span style="color: #007700">= </span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   <div class="example" id="example-2">
    <p><strong>Example #2 SQL statement template with question mark parameters</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/* Execute a prepared statement by passing an array of values */<br /></span><span style="color: #0000BB">$sth </span><span style="color: #007700">= </span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">'SELECT name, colour, calories<br />    FROM fruit<br />    WHERE calories &lt; ? AND colour = ?'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">([</span><span style="color: #0000BB">150</span><span style="color: #007700">, </span><span style="color: #DD0000">'red'</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$red </span><span style="color: #007700">= </span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">([</span><span style="color: #0000BB">175</span><span style="color: #007700">, </span><span style="color: #DD0000">'yellow'</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$yellow </span><span style="color: #007700">= </span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   <div class="example" id="example-3">
    <p><strong>Example #3 SQL statement template with question mark escaped</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/* note: this is only valid on PostgreSQL databases */<br /></span><span style="color: #0000BB">$sth </span><span style="color: #007700">= </span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">'SELECT * FROM issues WHERE tag::jsonb ?? ?'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">([</span><span style="color: #DD0000">'feature'</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$featureIssues </span><span style="color: #007700">= </span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">([</span><span style="color: #DD0000">'performance'</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$performanceIssues </span><span style="color: #007700">= </span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

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


 <div class="refsect1 seealso" id="refsect1-pdo.prepare-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="methodname"><a href="pdo.exec.php" class="methodname" rel="rdfs-seeAlso">PDO::exec()</a> - Execute an SQL statement and return the number of affected rows</span></li>
    <li><span class="methodname"><a href="pdo.query.php" class="methodname" rel="rdfs-seeAlso">PDO::query()</a> - Prepares and executes an SQL statement without placeholders</span></li>
    <li><span class="methodname"><a href="pdostatement.execute.php" class="methodname" rel="rdfs-seeAlso">PDOStatement::execute()</a> - Executes a prepared statement</span></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>