<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Prepares an SQL statement to be executed</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.db2-pconnect.html">db2_pconnect</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.db2-primary-keys.html">db2_primary_keys</a></div> <div class="up"><a href="ref.ibm-db2.html">IBM DB2 Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.db2-prepare" class="refentry"> <div class="refnamediv"> <h1 class="refname">db2_prepare</h1> <p class="verinfo">(PECL ibm_db2 >= 1.0.0)</p><p class="refpurpose"><span class="refname">db2_prepare</span> — <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="type">resource</span> <span class="methodname"><strong>db2_prepare</strong></span> ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$connection</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$statement</code></span> [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$options</code></span> ] )</div> <p class="para rdfs-comment"> <span class="function"><strong>db2_prepare()</strong></span> creates a prepared SQL statement which can include 0 or more parameter markers (<em>?</em> 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.html" 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.html" 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"> <p class="para"> <em class="emphasis">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.html" 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. </p> </li> <li class="listitem"> <p class="para"> <em class="emphasis">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. </p> </li> <li class="listitem"> <p class="para"> <em class="emphasis">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.html" class="function">db2_bind_param()</a></span>. </p> </li> </ul> </p> </div> <div class="refsect1 parameters" id="refsect1-function.db2-prepare-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">connection</code></dt> <dd> <p class="para"> A valid database connection resource variable as returned from <span class="function"><a href="function.db2-connect.html" class="function">db2_connect()</a></span> or <span class="function"><a href="function.db2-pconnect.html" class="function">db2_pconnect()</a></span>. </p> </dd> <dt> <code class="parameter">statement</code></dt> <dd> <p class="para"> An SQL statement, optionally containing one or more parameter markers.. </p> </dd> <dt> <code class="parameter">options</code></dt> <dd> <p class="para"> An associative array containing statement options. You can use this parameter to request a scrollable cursor on database servers that support this functionality. </p> <p class="para"> For a description of valid statement options, see <span class="function"><a href="function.db2-set-option.html" class="function">db2_set_option()</a></span>. </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.db2-prepare-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns a statement resource if the SQL statement was successfully parsed and prepared by the database server. Returns <strong><code>FALSE</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.html" class="function">db2_stmt_error()</a></span> or <span class="function"><a href="function.db2-stmt-errormsg.html" class="function">db2_stmt_errormsg()</a></span>. </p> </div> <div class="refsect1 examples" id="refsect1-function.db2-prepare-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-1303"> <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.html" class="function">db2_execute()</a></span>. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?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">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.db2-prepare-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><span class="function"><a href="function.db2-bind-param.html" class="function" rel="rdfs-seeAlso">db2_bind_param()</a> - Binds a PHP variable to an SQL statement parameter</span></li> <li class="member"><span class="function"><a href="function.db2-execute.html" class="function" rel="rdfs-seeAlso">db2_execute()</a> - Executes a prepared SQL statement</span></li> <li class="member"><span class="function"><a href="function.db2-stmt-error.html" class="function" rel="rdfs-seeAlso">db2_stmt_error()</a> - Returns a string containing the SQLSTATE returned by an SQL statement</span></li> <li class="member"><span class="function"><a href="function.db2-stmt-errormsg.html" class="function" rel="rdfs-seeAlso">db2_stmt_errormsg()</a> - Returns a string containing the last SQL statement error message</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.db2-pconnect.html">db2_pconnect</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.db2-primary-keys.html">db2_primary_keys</a></div> <div class="up"><a href="ref.ibm-db2.html">IBM DB2 Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>