<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.oci8.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.oci-execute.php',
    1 => 'oci_execute',
    2 => '执行语句',
  ),
  'up' => 
  array (
    0 => 'ref.oci8.php',
    1 => 'OCI8 函数',
  ),
  'prev' => 
  array (
    0 => 'function.oci-error.php',
    1 => 'oci_error',
  ),
  'next' => 
  array (
    0 => 'function.oci-fetch.php',
    1 => 'oci_fetch',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/oci8/functions/oci-execute.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.oci-execute" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">oci_execute</h1>
  <p class="verinfo">(PHP 5, PHP 7, PHP 8, PECL OCI8 &gt;= 1.1.0)</p><p class="refpurpose"><span class="refname">oci_execute</span> &mdash; <span class="dc-title">执行语句</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.oci-execute-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>oci_execute</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> <code class="parameter">$statement</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$mode</code><span class="initializer"> = <strong><code><a href="oci8.constants.php#constant.oci-commit-on-success">OCI_COMMIT_ON_SUCCESS</a></code></strong></span></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="para rdfs-comment">
   执行先前从 <span class="function"><a href="function.oci-parse.php" class="function">oci_parse()</a></span> 返回的 <code class="parameter">statement</code>。
  </p>
  <p class="para">
   执行后，像 <code class="literal">INSERT</code> 这样的语句默认会将数据提交到数据库中。对于像 <code class="literal">SELECT</code>
   这样的语句，执行查询的逻辑。随后可以使用 <span class="function"><a href="function.oci-fetch-array.php" class="function">oci_fetch_array()</a></span> 等函数在 PHP 中获取查询结果。
  </p>
  <p class="para">
   每个已解析的语句可能会执行多次，节省了重新解析的成本。当使用 <span class="function"><a href="function.oci-bind-by-name.php" class="function">oci_bind_by_name()</a></span> 绑定数据时，这通常用于 <code class="literal">INSERT</code> 语句。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.oci-execute-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">statement</code></dt>
     <dd>
      <p class="para">
       有效的 OCI 语句标识符。
      </p>
     </dd>
    
    
     <dt><code class="parameter">mode</code></dt>
     <dd>
      <p class="para">
        可选的第二个参数可以是以下常量之一：
       <table class="doctable table">
        <caption><strong>执行模式</strong></caption>
        
         <thead>
          <tr>
           <th>常量</th>
           <th>说明</th>
          </tr>

         </thead>

         <tbody class="tbody">
          <tr>
           <td><strong><code><a href="oci8.constants.php#constant.oci-commit-on-success">OCI_COMMIT_ON_SUCCESS</a></code></strong></td>
           <td>当语句成功时，自动提交此连接的所有未完成更改。这是默认设置。</td>
          </tr>

          <tr>
           <td><strong><code><a href="oci8.constants.php#constant.oci-describe-only">OCI_DESCRIBE_ONLY</a></code></strong></td>
           <td>使查询元数据可用于 <span class="function"><a href="function.oci-field-name.php" class="function">oci_field_name()</a></span> 等函数，但不创建结果集。任何后续的读取调用（例如
           <span class="function"><a href="function.oci-fetch-array.php" class="function">oci_fetch_array()</a></span>）都将失败。</td>
          </tr>

          <tr>
           <td><strong><code><a href="oci8.constants.php#constant.oci-no-auto-commit">OCI_NO_AUTO_COMMIT</a></code></strong></td>
           <td>不要自动提交更改。</td>
          </tr>

         </tbody>
        
       </table>

      </p>
      <p class="para">
       使用 <strong><code><a href="oci8.constants.php#constant.oci-no-auto-commit">OCI_NO_AUTO_COMMIT</a></code></strong> 模式启动或继续事务。当连接关闭或脚本结束时，事务会自动回滚。显式调用
       <span class="function"><a href="function.oci-commit.php" class="function">oci_commit()</a></span> 来提交事务，或调用 <span class="function"><a href="function.oci-rollback.php" class="function">oci_rollback()</a></span> 来中止它。
      </p>
      <p class="para">
       插入或更新数据时，出于关系数据一致性和性能原因，建议使用事务。
      </p>
      <p class="para">
       If <strong><code><a href="oci8.constants.php#constant.oci-no-auto-commit">OCI_NO_AUTO_COMMIT</a></code></strong> mode is used for any
       statement including queries, and 
        <span class="function"><a href="function.oci-commit.php" class="function">oci_commit()</a></span>
       or <span class="function"><a href="function.oci-rollback.php" 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><a href="oci8.constants.php#constant.oci-no-auto-commit">OCI_NO_AUTO_COMMIT</a></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>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.oci-execute-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>， 或者在失败时返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.oci-execute-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 <span class="function"><strong>oci_execute()</strong></span> 用于查询</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">"&lt;table border='1'&gt;\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">"&lt;tr&gt;\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">"    &lt;td&gt;" </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">"&amp;nbsp;"</span><span style="color: #007700">) . </span><span style="color: #DD0000">"&lt;/td&gt;\n"</span><span style="color: #007700">;<br />    }<br />    echo </span><span style="color: #DD0000">"&lt;/tr&gt;\n"</span><span style="color: #007700">;<br />}<br />echo </span><span style="color: #DD0000">"&lt;/table&gt;\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>示例 #2 <span class="function"><strong>oci_execute()</strong></span> 未指定模式示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3">
    <p><strong>示例 #3 <span class="function"><strong>oci_execute()</strong></span> 和 <strong><code><a href="oci8.constants.php#constant.oci-no-auto-commit">OCI_NO_AUTO_COMMIT</a></code></strong> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">&lt;= </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">);<br />}<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">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4">
    <p><strong>示例 #4 具有不同提交模式的 <span class="function"><strong>oci_execute()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5">
    <p><strong>示例 #5 <span class="function"><strong>oci_execute()</strong></span> 与
     <strong><code><a href="oci8.constants.php#constant.oci-describe-only">OCI_DESCRIBE_ONLY</a></code></strong> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">&lt;= </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">"&lt;br&gt;\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.oci-execute-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="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.php" 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><a href="oci8.constants.php#constant.oci-commit-on-success">OCI_COMMIT_ON_SUCCESS</a></code></strong> mode explicitly or by
    default will commit any previous uncommitted transaction.
   </p>
   <p class="para">
    Any Oracle DDL statement such as <code class="literal">CREATE</code>
    or <code class="literal">DROP</code> will automatically commit any
    uncommitted transaction.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="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.php" 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">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.oci-parse.php" class="function" rel="rdfs-seeAlso">oci_parse()</a> - 预处理用于执行的 Oracle 语句</span></li>
   </ul>
  </p>
 </div>


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