<!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 statement</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.oci-error.html">oci_error</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.oci-fetch-all.html">oci_fetch_all</a></div> <div class="up"><a href="ref.oci8.html">OCI8 Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.oci-execute" class="refentry"> <div class="refnamediv"> <h1 class="refname">oci_execute</h1> <p class="verinfo">(PHP 5, PECL OCI8 >= 1.1.0)</p><p class="refpurpose"><span class="refname">oci_execute</span> — <span class="dc-title">Executes a statement</span></p> </div> <div class="refsect1 description" id="refsect1-function.oci-execute-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><strong>oci_execute</strong></span> ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$statement</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$mode</code><span class="initializer"> = OCI_COMMIT_ON_SUCCESS</span></span> ] )</div> <p class="para rdfs-comment"> Executes a <em><code class="parameter">statement</code></em> previously returned from <span class="function"><a href="function.oci-parse.html" class="function">oci_parse()</a></span>. </p> <p class="para"> After execution, statements like <em>INSERT</em> will have data committed to the database by default. For statements like <em>SELECT</em>, execution performs the logic of the query. Query results can subsequently be fetched in PHP with functions like <span class="function"><a href="function.oci-fetch-array.html" class="function">oci_fetch_array()</a></span>. </p> <p class="para"> Each parsed statement may be executed multiple times, saving the cost of re-parsing. This is commonly used for <em>INSERT</em> statements when data is bound with <span class="function"><a href="function.oci-bind-by-name.html" class="function">oci_bind_by_name()</a></span>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.oci-execute-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">statement</code></em></span> <dd> <p class="para"> A valid OCI statement identifier. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">mode</code></em></span> <dd> <p class="para"> An optional second parameter can be one of the following constants: <table class="doctable table"> <caption><strong>Execution Modes</strong></caption> <thead> <tr> <th>Constant</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td><strong><code>OCI_COMMIT_ON_SUCCESS</code></strong></td> <td>Automatically commit all outstanding changes for this connection when the statement has succeeded. This is the default.</td> </tr> <tr> <td><strong><code>OCI_DESCRIBE_ONLY</code></strong></td> <td>Make query meta data available to functions like <span class="function"><a href="function.oci-field-name.html" class="function">oci_field_name()</a></span> but do not create a result set. Any subsequent fetch call such as <span class="function"><a href="function.oci-fetch-array.html" class="function">oci_fetch_array()</a></span> will fail.</td> </tr> <tr> <td><strong><code>OCI_NO_AUTO_COMMIT</code></strong></td> <td>Do not automatically commit changes. Prior to PHP 5.3.2 (PECL OCI8 1.4) use <strong><code>OCI_DEFAULT</code></strong> which is equivalent to <strong><code>OCI_NO_AUTO_COMMIT</code></strong>.</td> </tr> </tbody> </table> </p> <p class="para"> Using <strong><code>OCI_NO_AUTO_COMMIT</code></strong> mode starts or continues a transaction. Transactions are automatically rolled back when the connection is closed, or when the script ends. Explicitly call <span class="function"><a href="function.oci-commit.html" class="function">oci_commit()</a></span> to commit a transaction, or <span class="function"><a href="function.oci-rollback.html" class="function">oci_rollback()</a></span> to abort it. </p> <p class="para"> When inserting or updating data, using transactions is recommended for relational data consistency and for performance reasons. </p> <p class="para"> If <strong><code>OCI_NO_AUTO_COMMIT</code></strong> mode is used for any statement including queries, and <span class="function"><a href="function.oci-commit.html" class="function">oci_commit()</a></span> or <span class="function"><a href="function.oci-rollback.html" class="function">oci_rollback()</a></span> is not subsequently called, then OCI8 will perform a rollback at the end of the script even if no data was changed. To avoid an unnecessary rollback, many scripts do not use <strong><code>OCI_NO_AUTO_COMMIT</code></strong> mode for queries or PL/SQL. Be careful to ensure the appropriate transactional consistency for the application when using <span class="function"><strong>oci_execute()</strong></span> with different modes in the same script. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.oci-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.oci-execute-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-1973"> <p><strong>Example #1 <span class="function"><strong>oci_execute()</strong></span> for queries</strong></p> <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">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">, </span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">, </span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$stid </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #DD0000">'SELECT * FROM employees'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"<table border='1'>\n"</span><span style="color: #007700">;<br />while (</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_fetch_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">, </span><span style="color: #0000BB">OCI_ASSOC</span><span style="color: #007700">+</span><span style="color: #0000BB">OCI_RETURN_NULLS</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"<tr>\n"</span><span style="color: #007700">;<br /> foreach (</span><span style="color: #0000BB">$row </span><span style="color: #007700">as </span><span style="color: #0000BB">$item</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">" <td>" </span><span style="color: #007700">. (</span><span style="color: #0000BB">$item </span><span style="color: #007700">!== </span><span style="color: #0000BB">null </span><span style="color: #007700">? </span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$item</span><span style="color: #007700">, </span><span style="color: #0000BB">ENT_QUOTES</span><span style="color: #007700">) : </span><span style="color: #DD0000">"&nbsp;"</span><span style="color: #007700">) . </span><span style="color: #DD0000">"</td>\n"</span><span style="color: #007700">;<br /> }<br /> echo </span><span style="color: #DD0000">"</tr>\n"</span><span style="color: #007700">;<br />}<br />echo </span><span style="color: #DD0000">"</table>\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-1974"> <p><strong>Example #2 <span class="function"><strong>oci_execute()</strong></span> without specifying a mode example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">// Before running, create the table:<br />// CREATE TABLE MYTABLE (col1 NUMBER);<br /><br /></span><span style="color: #0000BB">$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">, </span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">, </span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$stid </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #DD0000">'INSERT INTO mytab (col1) VALUES (123)'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">); </span><span style="color: #FF8000">// The row is committed and immediately visible to other users<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-1975"> <p><strong>Example #3 <span class="function"><strong>oci_execute()</strong></span> with <strong><code>OCI_NO_AUTO_COMMIT</code></strong> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">// Before running, create the table:<br />// CREATE TABLE MYTABLE (col1 NUMBER);<br /><br /></span><span style="color: #0000BB">$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">, </span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">, </span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$stid </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #DD0000">'INSERT INTO mytab (col1) VALUES (:bv)'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">oci_bind_by_name</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">, </span><span style="color: #DD0000">':bv'</span><span style="color: #007700">, </span><span style="color: #0000BB">$i</span><span style="color: #007700">, </span><span style="color: #0000BB">10</span><span style="color: #007700">);<br />for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700"><= </span><span style="color: #0000BB">5</span><span style="color: #007700">; ++</span><span style="color: #0000BB">$i</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">, </span><span style="color: #0000BB">OCI_NO_AUTO_COMMIT</span><span style="color: #007700">); </span><span style="color: #FF8000">// use OCI_DEFAULT for PHP <= 5.3.1<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">oci_commit</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">); </span><span style="color: #FF8000">// commits all new values: 1, 2, 3, 4, 5<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-1976"> <p><strong>Example #4 <span class="function"><strong>oci_execute()</strong></span> with different commit modes example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">// Before running, create the table:<br />// CREATE TABLE MYTABLE (col1 NUMBER);<br /><br /></span><span style="color: #0000BB">$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">, </span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">, </span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$stid </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #DD0000">'INSERT INTO mytab (col1) VALUES (123)'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">, </span><span style="color: #0000BB">OCI_NO_AUTO_COMMIT</span><span style="color: #007700">); </span><span style="color: #FF8000">// data not committed<br /><br /></span><span style="color: #0000BB">$stid </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #DD0000">'INSERT INTO mytab (col1) VALUES (456)'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">); </span><span style="color: #FF8000">// commits both 123 and 456 values<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-1977"> <p><strong>Example #5 <span class="function"><strong>oci_execute()</strong></span> with <strong><code>OCI_DESCRIBE_ONLY</code></strong> example</strong></p> <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">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">, </span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">, </span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$stid </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #DD0000">'SELECT * FROM locations'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$s</span><span style="color: #007700">, </span><span style="color: #0000BB">OCI_DESCRIBE_ONLY</span><span style="color: #007700">);<br />for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700"><= </span><span style="color: #0000BB">oci_num_fields</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">); ++</span><span style="color: #0000BB">$i</span><span style="color: #007700">) {<br /> echo </span><span style="color: #0000BB">oci_field_name</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">, </span><span style="color: #0000BB">$i</span><span style="color: #007700">) . </span><span style="color: #DD0000">"<br>\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.oci-execute-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Transactions are automatically rolled back when connections are closed, or when the script ends, whichever is soonest. Explicitly call <span class="function"><a href="function.oci-commit.html" class="function">oci_commit()</a></span> to commit a transaction. </p> <p class="para"> Any call to <span class="function"><strong>oci_execute()</strong></span> that uses <strong><code>OCI_COMMIT_ON_SUCCESS</code></strong> mode explicitly or by default will commit any previous uncommitted transaction. </p> <p class="para"> Any Oracle DDL statement such as <em>CREATE</em> or <em>DROP</em> will automatically commit any uncommitted transaction. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Because the <span class="function"><strong>oci_execute()</strong></span> function generally sends the statement to the database, <span class="function"><strong>oci_execute()</strong></span> can identify some statement syntax errors that the lightweight, local <span class="function"><a href="function.oci-parse.html" class="function">oci_parse()</a></span> function does not. </p> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.oci-execute-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.oci-parse.html" class="function" rel="rdfs-seeAlso">oci_parse()</a> - Prepares an Oracle statement for execution</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.oci-error.html">oci_error</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.oci-fetch-all.html">oci_fetch_all</a></div> <div class="up"><a href="ref.oci8.html">OCI8 Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>