Source of: /manual/en/pdo.prepare.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.prepare.php',
1 => 'PDO::prepare',
),
'up' =>
array (
0 => 'class.pdo.php',
1 => 'The PDO class',
),
'prev' =>
array (
0 => 'pdo.lastinsertid.php',
1 => 'PDO::lastInsertId',
),
'next' =>
array (
0 => 'pdo.query.php',
1 => 'PDO::query',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="pdo.prepare" class="refentry">
<div class="refnamediv">
<h1 class="refname">PDO::prepare</h1>
<p class="verinfo">(PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)</p><p class="refpurpose"><span class="refname">PDO::prepare</span> — <span class="dc-title">
Prepares a statement for execution and returns a statement object
</span></p>
</div>
<a name="pdo.prepare.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::prepare</b></span>
( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$statement</tt></span>
[, <span class="methodparam"><span class="type">array</span> <tt class="parameter">$driver_options</tt><span class="initializer"> = array()</span></span>
] )</div>
<p class="para rdfs-comment">
Prepares an SQL statement to be executed by the
<a href="pdostatement.execute.php" class="function">PDOStatement::execute()</a> method. The SQL statement can
contain zero or more named (:name) or question mark (?) parameter markers
for which real values will be substituted when the statement is executed.
You cannot use both named and question mark parameter markers within the same
SQL statement; pick 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 <a href="pdostatement.execute.php" class="function">PDOStatement::execute()</a>.
You cannot use a named parameter marker of the same name twice in a prepared
statement. You cannot bind multiple values to a single named parameter in,
for example, the IN() clause of an SQL statement.
</p>
<p class="para">
Calling <b>PDO::prepare()</b> and
<a href="pdostatement.execute.php" class="function">PDOStatement::execute()</a> 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,
and helps to prevent SQL injection attacks by eliminating the need to
manually quote 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>
</div>
<a name="pdo.prepare.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">
This must be a valid SQL statement for the target database server.
</p>
</dd>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">driver_options</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
This array holds one or more key=>value pairs to set
attribute values for the PDOStatement object that this method
returns. You would most commonly use this to set the
<i>PDO::ATTR_CURSOR</i> value to
<i>PDO::CURSOR_SCROLL</i> to request a scrollable cursor.
Some drivers have driver specific options that may be set at
prepare-time.
</p>
</dd>
</dl>
<p>
</p>
</div>
<a name="pdo.prepare.returnvalues"></a><div class="refsect1 returnvalues">
<h3 class="title">Return Values</h3>
<p class="para">
If the database server successfully prepares the statement,
<b>PDO::prepare()</b> returns a
<a href="class.pdostatement.php" class="classname">PDOStatement</a> object.
If the database server cannot successfully prepare the statement,
<b>PDO::prepare()</b> returns <b><tt class="constant">FALSE</tt></b> or emits
<a href="class.pdoexception.php" class="classname">PDOException</a> (depending on <a href="pdo.error-handling.php" class="link">error handling</a>).
</p>
<blockquote><p><b class="note">Note</b>:
Emulated prepared statements does not communicate with the database server
so <b>PDO::prepare()</b> does not check the statement.
<br />
</p></blockquote>
</div>
<a name="pdo.prepare.examples"></a><div class="refsect1 examples">
<h3 class="title">Examples</h3>
<p class="para">
</p><div class="example"><p><b>Example #1 Prepare an SQL statement with named parameters</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?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 < :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">-></span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #0000BB">$sql</span><span style="color: #007700">, array(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">ATTR_CURSOR </span><span style="color: #007700">=> </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">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">(array(</span><span style="color: #DD0000">':calories' </span><span style="color: #007700">=> </span><span style="color: #0000BB">150</span><span style="color: #007700">, </span><span style="color: #DD0000">':colour' </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">-></span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'calories' </span><span style="color: #007700">=> </span><span style="color: #0000BB">175</span><span style="color: #007700">, </span><span style="color: #DD0000">'colour' </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">-></span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p><div class="example">
<p><b>Example #2 Prepare an SQL statement with question mark parameters</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?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">-></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 < ? AND colour = ?'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">(array(</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">-></span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">(array(</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">-></span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
</div>
<a name="pdo.prepare.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.query.php" class="function" rel="rdfs-seeAlso">PDO::query()</a> - Executes an SQL statement, returning a result set as a PDOStatement 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(); ?>