<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.ibm-db2.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.db2-prepare.php',
    1 => 'db2_prepare',
    2 => 'Prepares an SQL statement to be executed',
  ),
  'up' => 
  array (
    0 => 'ref.ibm-db2.php',
    1 => 'IBM DB2 Functions',
  ),
  'prev' => 
  array (
    0 => 'function.db2-pconnect.php',
    1 => 'db2_pconnect',
  ),
  'next' => 
  array (
    0 => 'function.db2-primary-keys.php',
    1 => 'db2_primary_keys',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/ibm_db2/functions/db2-prepare.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.db2-prepare" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">db2_prepare</h1>
  <p class="verinfo">(PECL ibm_db2 &gt;= 1.0.0)</p><p class="refpurpose"><span class="refname">db2_prepare</span> &mdash; <span class="dc-title">
   Prepares an SQL statement to be executed
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.db2-prepare-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>db2_prepare</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> <code class="parameter">$connection</code></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$statement</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="language.types.resource.php" class="type resource">resource</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>



  <p class="simpara">
   <span class="function"><strong>db2_prepare()</strong></span> creates a prepared SQL statement which can
   include 0 or more parameter markers (<code class="literal">?</code> characters)
   representing parameters for input, output, or input/output. You can pass
   parameters to the prepared statement using
   <span class="function"><a href="function.db2-bind-param.php" class="function">db2_bind_param()</a></span>, or for input values only, as an array
   passed to <span class="function"><a href="function.db2-execute.php" class="function">db2_execute()</a></span>.
  </p>
  <p class="para">
   There are three main advantages to using prepared statements in your
   application:
   <ul class="itemizedlist">
    <li class="listitem">
     <span class="simpara">
      <em>Performance</em>: when you prepare a statement, the
      database server creates an optimized access plan for retrieving data with
      that statement. Subsequently issuing the prepared statement with
      <span class="function"><a href="function.db2-execute.php" class="function">db2_execute()</a></span> enables the statements to reuse that
      access plan and avoids the overhead of dynamically creating a new access
      plan for every statement you issue.
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <em>Security</em>: when you prepare a statement, you can
      include parameter markers for input values. When you execute a prepared
      statement with input values for placeholders, the database server checks
      each input value to ensure that the type matches the column definition or
      parameter definition.
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <em>Advanced functionality</em>: Parameter markers not only
      enable you to pass input values to prepared SQL statements, they also
      enable you to retrieve OUT and INOUT parameters from stored procedures
      using <span class="function"><a href="function.db2-bind-param.php" class="function">db2_bind_param()</a></span>.
     </span>
    </li>
   </ul>
  </p>

 </div>

 <div class="refsect1 parameters" id="refsect1-function.db2-prepare-parameters">
  <h3 class="title">Parameters</h3>
  <dl>
   
    <dt><code class="parameter">connection</code></dt>
    <dd>
     <span class="simpara">
      A valid database connection resource variable as returned from
      <span class="function"><a href="function.db2-connect.php" class="function">db2_connect()</a></span> or <span class="function"><a href="function.db2-pconnect.php" class="function">db2_pconnect()</a></span>.
     </span>
    </dd>
   
   
    <dt><code class="parameter">statement</code></dt>
    <dd>
     <span class="simpara">
      An SQL statement, optionally containing one or more parameter markers..
     </span>
    </dd>
   
   
    <dt><code class="parameter">options</code></dt>
    <dd>
     <span class="simpara">
      An associative array containing statement options. You can use this
      parameter to request a scrollable cursor on database servers that
      support this functionality.
     </span>
     <span class="simpara">
      For a description of valid statement options, see
      <span class="function"><a href="function.db2-set-option.php" class="function">db2_set_option()</a></span>.
     </span>
    </dd>
   
  </dl>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.db2-prepare-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="simpara">
   Returns a statement resource if the SQL statement was successfully parsed and
   prepared by the database server. Returns <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> if the database server
   returned an error. You can determine which error was returned by calling
   <span class="function"><a href="function.db2-stmt-error.php" class="function">db2_stmt_error()</a></span> or <span class="function"><a href="function.db2-stmt-errormsg.php" class="function">db2_stmt_errormsg()</a></span>.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.db2-prepare-examples">
  <h3 class="title">Examples</h3>
  <div class="example" id="example-1">
   <p><strong>Example #1 Preparing and executing an SQL statement with parameter markers</strong></p>
   <div class="example-contents"><p>
    The following example prepares an INSERT statement that accepts four
    parameter markers, then iterates over an array of arrays containing the
    input values to be passed to <span class="function"><a href="function.db2-execute.php" class="function">db2_execute()</a></span>.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$animals </span><span style="color: #007700">= array(<br />    array(</span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #DD0000">'cat'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Pook'</span><span style="color: #007700">, </span><span style="color: #0000BB">3.2</span><span style="color: #007700">),<br />    array(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">'dog'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Peaches'</span><span style="color: #007700">, </span><span style="color: #0000BB">12.3</span><span style="color: #007700">),<br />    array(</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #DD0000">'horse'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Smarty'</span><span style="color: #007700">, </span><span style="color: #0000BB">350.0</span><span style="color: #007700">),<br />);<br /><br /></span><span style="color: #0000BB">$insert </span><span style="color: #007700">= </span><span style="color: #DD0000">'INSERT INTO animals (id, breed, name, weight)<br />    VALUES (?, ?, ?, ?)'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$stmt </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_prepare</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #0000BB">$insert</span><span style="color: #007700">);<br />if (</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">) {<br />    foreach (</span><span style="color: #0000BB">$animals </span><span style="color: #007700">as </span><span style="color: #0000BB">$animal</span><span style="color: #007700">) {<br />        </span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">, </span><span style="color: #0000BB">$animal</span><span style="color: #007700">);<br />    }<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.db2-prepare-seealso">
  <h3 class="title">See Also</h3>
  <ul class="simplelist">
   <li><span class="function"><a href="function.db2-bind-param.php" class="function" rel="rdfs-seeAlso">db2_bind_param()</a> - Binds a PHP variable to an SQL statement parameter</span></li>
   <li><span class="function"><a href="function.db2-execute.php" class="function" rel="rdfs-seeAlso">db2_execute()</a> - Executes a prepared SQL statement</span></li>
   <li><span class="function"><a href="function.db2-stmt-error.php" class="function" rel="rdfs-seeAlso">db2_stmt_error()</a> - Returns a string containing the SQLSTATE returned by an SQL statement</span></li>
   <li><span class="function"><a href="function.db2-stmt-errormsg.php" class="function" rel="rdfs-seeAlso">db2_stmt_errormsg()</a> - Returns a string containing the last SQL statement error message</span></li>
  </ul>
 </div>


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