<!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>Executes a prepared SQL statement</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.db2-exec.html">db2_exec</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.db2-fetch-array.html">db2_fetch_array</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-execute" class="refentry"> <div class="refnamediv"> <h1 class="refname">db2_execute</h1> <p class="verinfo">(PECL ibm_db2 >= 1.0.0)</p><p class="refpurpose"><span class="refname">db2_execute</span> — <span class="dc-title"> Executes a prepared SQL statement </span></p> </div> <div class="refsect1 description" id="refsect1-function.db2-execute-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><strong>db2_execute</strong></span> ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$stmt</code></span> [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$parameters</code></span> ] )</div> <p class="para rdfs-comment"> <span class="function"><strong>db2_execute()</strong></span> executes an SQL statement that was prepared by <span class="function"><a href="function.db2-prepare.html" class="function">db2_prepare()</a></span>. </p> <p class="para"> If the SQL statement returns a result set, for example, a SELECT statement or a CALL to a stored procedure that returns one or more result sets, you can retrieve a row as an array from the <em>stmt</em> resource using <span class="function"><a href="function.db2-fetch-assoc.html" class="function">db2_fetch_assoc()</a></span>, <span class="function"><a href="function.db2-fetch-both.html" class="function">db2_fetch_both()</a></span>, or <span class="function"><a href="function.db2-fetch-array.html" class="function">db2_fetch_array()</a></span>. Alternatively, you can use <span class="function"><a href="function.db2-fetch-row.html" class="function">db2_fetch_row()</a></span> to move the result set pointer to the next row and fetch a column at a time from that row with <span class="function"><a href="function.db2-result.html" class="function">db2_result()</a></span>. </p> <p class="para"> Refer to <span class="function"><a href="function.db2-prepare.html" class="function">db2_prepare()</a></span> for a brief discussion of the advantages of using <span class="function"><a href="function.db2-prepare.html" class="function">db2_prepare()</a></span> and <span class="function"><strong>db2_execute()</strong></span> rather than <span class="function"><a href="function.db2-exec.html" class="function">db2_exec()</a></span>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.db2-execute-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">stmt</code></em></span> <dd> <p class="para"> A prepared statement returned from <span class="function"><a href="function.db2-prepare.html" class="function">db2_prepare()</a></span>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">parameters</code></em></span> <dd> <p class="para"> An array of input parameters matching any parameter markers contained in the prepared statement. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.db2-execute-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> on success or <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 examples" id="refsect1-function.db2-execute-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-1158"> <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"><strong>db2_execute()</strong></span>. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$pet </span><span style="color: #007700">= 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 /><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 /><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 /> </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">$pet</span><span style="color: #007700">);<br /> if (</span><span style="color: #0000BB">$result</span><span style="color: #007700">) {<br /> print </span><span style="color: #DD0000">"Successfully added new pet."</span><span style="color: #007700">;<br /> }<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Successfully added new pet. </pre></div> </div> </div> <div class="example" id="example-1159"> <p><strong>Example #2 Calling a stored procedure with an OUT parameter</strong></p> <div class="example-contents"><p> The following example prepares a CALL statement that accepts one parameter marker representing an OUT parameter, binds the PHP variable <em>$my_pets</em> to the parameter using <span class="function"><a href="function.db2-bind-param.html" class="function">db2_bind_param()</a></span>, then issues <span class="function"><strong>db2_execute()</strong></span> to execute the CALL statement. After the CALL to the stored procedure has been made, the value of <em>$num_pets</em> changes to reflect the value returned by the stored procedure for that OUT parameter. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$num_pets </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res </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: #DD0000">"CALL count_my_pets(?)"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$rc </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_bind_param</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">"num_pets"</span><span style="color: #007700">, </span><span style="color: #0000BB">DB2_PARAM_OUT</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$rc </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">);<br />print </span><span style="color: #DD0000">"I have </span><span style="color: #0000BB">$num_pets</span><span style="color: #DD0000"> pets!"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> I have 7 pets! </pre></div> </div> </div> <div class="example" id="example-1160"> <p><strong>Example #3 Returning XML data as an SQL ResultSet</strong></p> <div class="example-contents"><p> The following example demonstrates how to work with documents stored in a XML column using the SAMPLE database. Using some pretty simple SQL/XML, this example returns some of the nodes in a XML document in an SQL ResultSet format that most users are familiar with. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"SAMPLE"</span><span style="color: #007700">, </span><span style="color: #DD0000">"db2inst1"</span><span style="color: #007700">, </span><span style="color: #DD0000">"ibmdb2"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$query </span><span style="color: #007700">= </span><span style="color: #DD0000">'SELECT * FROM XMLTABLE(<br /> XMLNAMESPACES (DEFAULT \'http://posample.org\'),<br /> \'db2-fn:xmlcolumn("CUSTOMER.INFO")/customerinfo\'<br /> COLUMNS<br /> "CID" VARCHAR (50) PATH \'@Cid\',<br /> "NAME" VARCHAR (50) PATH \'name\',<br /> "PHONE" VARCHAR (50) PATH \'phone [ @type = "work"]\'<br /> ) AS T<br /> WHERE NAME = ?<br /> '</span><span style="color: #007700">;<br /><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">$query</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$name </span><span style="color: #007700">= </span><span style="color: #DD0000">'Kathy Smith'</span><span style="color: #007700">;<br /><br />if (</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">db2_bind_param</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">"name"</span><span style="color: #007700">, </span><span style="color: #0000BB">DB2_PARAM_IN</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">db2_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">);<br /><br /> while(</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_fetch_object</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">)){<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$row</span><span style="color: #007700">-></span><span style="color: #0000BB">CID</span><span style="color: #DD0000"> </span><span style="color: #0000BB">$row</span><span style="color: #007700">-></span><span style="color: #0000BB">NAME</span><span style="color: #DD0000"> </span><span style="color: #0000BB">$row</span><span style="color: #007700">-></span><span style="color: #0000BB">PHONE</span><span style="color: #DD0000">\n"</span><span style="color: #007700">);<br /> }<br />}<br /></span><span style="color: #0000BB">db2_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> 1000 Kathy Smith 416-555-1358 1001 Kathy Smith 905-555-7258 </pre></div> </div> </div> <div class="example" id="example-1161"> <p><strong>Example #4 Performing a "JOIN" with XML data</strong></p> <div class="example-contents"><p> The following example works with documents stored in 2 different XML columns in the SAMPLE database. It creates 2 temporary tables from the XML documents from 2 different columns and returns an SQL ResultSet with information regarding shipping status for the customer. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"SAMPLE"</span><span style="color: #007700">, </span><span style="color: #DD0000">"db2inst1"</span><span style="color: #007700">, </span><span style="color: #DD0000">"ibmdb2"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$query </span><span style="color: #007700">= </span><span style="color: #DD0000">'<br />SELECT A.CID, A.NAME, A.PHONE, C.PONUM, C.STATUS<br />FROM<br />XMLTABLE(<br />XMLNAMESPACES (DEFAULT \'http://posample.org\'),<br />\'db2-fn:xmlcolumn("CUSTOMER.INFO")/customerinfo\'<br />COLUMNS<br />"CID" BIGINT PATH \'@Cid\',<br />"NAME" VARCHAR (50) PATH \'name\',<br />"PHONE" VARCHAR (50) PATH \'phone [ @type = "work"]\'<br />) as A,<br />PURCHASEORDER AS B,<br />XMLTABLE (<br />XMLNAMESPACES (DEFAULT \'http://posample.org\'),<br />\'db2-fn:xmlcolumn("PURCHASEORDER.PORDER")/PurchaseOrder\'<br />COLUMNS<br />"PONUM" BIGINT PATH \'@PoNum\',<br />"STATUS" VARCHAR (50) PATH \'@Status\'<br />) as C<br />WHERE A.CID = B.CUSTID AND<br /> B.POID = C.PONUM AND<br /> A.NAME = ?<br />'</span><span style="color: #007700">;<br /><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">$query</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$name </span><span style="color: #007700">= </span><span style="color: #DD0000">'Kathy Smith'</span><span style="color: #007700">;<br /><br />if (</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">db2_bind_param</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">"name"</span><span style="color: #007700">, </span><span style="color: #0000BB">DB2_PARAM_IN</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">db2_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">);<br /><br /> while(</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_fetch_object</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">)){<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$row</span><span style="color: #007700">-></span><span style="color: #0000BB">CID</span><span style="color: #DD0000"> </span><span style="color: #0000BB">$row</span><span style="color: #007700">-></span><span style="color: #0000BB">NAME</span><span style="color: #DD0000"> </span><span style="color: #0000BB">$row</span><span style="color: #007700">-></span><span style="color: #0000BB">PHONE</span><span style="color: #DD0000"> </span><span style="color: #0000BB">$row</span><span style="color: #007700">-></span><span style="color: #0000BB">PONUM</span><span style="color: #DD0000"> </span><span style="color: #0000BB">$row</span><span style="color: #007700">-></span><span style="color: #0000BB">STATUS</span><span style="color: #DD0000">\n"</span><span style="color: #007700">);<br /> }<br />}<br /><br /></span><span style="color: #0000BB">db2_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> 1001 Kathy Smith 905-555-7258 5002 Shipped </pre></div> </div> </div> <div class="example" id="example-1162"> <p><strong>Example #5 Returning SQL data as part of a larger XML document</strong></p> <div class="example-contents"><p> The following example works with a portion of the PRODUCT.DESCRIPTION documents in the SAMPLE database. It creates a XML document containing product description (XML data) and pricing info (SQL data). </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"SAMPLE"</span><span style="color: #007700">, </span><span style="color: #DD0000">"db2inst1"</span><span style="color: #007700">, </span><span style="color: #DD0000">"ibmdb2"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$query </span><span style="color: #007700">= </span><span style="color: #DD0000">'<br />SELECT<br />XMLSERIALIZE(<br />XMLQUERY(\'<br /> declare boundary-space strip;<br /> declare default element namespace "http://posample.org";<br /> <promoList> {<br /> for $prod in $doc/product<br /> where $prod/description/price < 10.00<br /> order by $prod/description/price ascending<br /> return(<br /> <promoitem> {<br /> $prod,<br /> <startdate> {$start} </startdate>,<br /> <enddate> {$end} </enddate>,<br /> <promoprice> {$promo} </promoprice><br /> } </promoitem><br /> )<br /> } </promoList><br />\' passing by ref DESCRIPTION AS "doc",<br />PROMOSTART as "start",<br />PROMOEND as "end",<br />PROMOPRICE as "promo"<br />RETURNING SEQUENCE)<br />AS CLOB (32000))<br />AS NEW_PRODUCT_INFO<br />FROM PRODUCT<br />WHERE PID = ?<br />'</span><span style="color: #007700">;<br /><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">$query</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$pid </span><span style="color: #007700">= </span><span style="color: #DD0000">"100-100-01"</span><span style="color: #007700">;<br /><br />if (</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">db2_bind_param</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">"pid"</span><span style="color: #007700">, </span><span style="color: #0000BB">DB2_PARAM_IN</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">db2_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">);<br /><br /> while(</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_fetch_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">)){<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]</span><span style="color: #DD0000">\n"</span><span style="color: #007700">);<br /> }<br />}<br /><br /></span><span style="color: #0000BB">db2_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> <promoList xmlns="http://posample.org"> <promoitem> <product pid="100-100-01"> <description> <name>Snow Shovel, Basic 22 inch</name> <details>Basic Snow Shovel, 22 inches wide, straight handle with D-Grip</details> <price>9.99</price> <weight>1 kg</weight> </description> </product> <startdate>2004-11-19</startdate> <enddate>2004-12-19</enddate> <promoprice>7.25</promoprice> </promoitem> </promoList> </pre></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.db2-execute-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.db2-exec.html" class="function" rel="rdfs-seeAlso">db2_exec()</a> - Executes an SQL statement directly</span></li> <li class="member"> <span class="function"><a href="function.db2-fetch-array.html" class="function" rel="rdfs-seeAlso">db2_fetch_array()</a> - Returns an array, indexed by column position, representing a row in a result set</span></li> <li class="member"> <span class="function"><a href="function.db2-fetch-assoc.html" class="function" rel="rdfs-seeAlso">db2_fetch_assoc()</a> - Returns an array, indexed by column name, representing a row in a result set</span></li> <li class="member"> <span class="function"><a href="function.db2-fetch-both.html" class="function" rel="rdfs-seeAlso">db2_fetch_both()</a> - Returns an array, indexed by both column name and position, representing a row in a result set</span></li> <li class="member"> <span class="function"><a href="function.db2-fetch-row.html" class="function" rel="rdfs-seeAlso">db2_fetch_row()</a> - Sets the result set pointer to the next row or requested row</span></li> <li class="member"> <span class="function"><a href="function.db2-prepare.html" class="function" rel="rdfs-seeAlso">db2_prepare()</a> - Prepares an SQL statement to be executed</span></li> <li class="member"> <span class="function"><a href="function.db2-result.html" class="function" rel="rdfs-seeAlso">db2_result()</a> - Returns a single column from a row in the result set</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-exec.html">db2_exec</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.db2-fetch-array.html">db2_fetch_array</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>