mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-18 05:26:57 +08:00
277 lines
22 KiB
HTML
277 lines
22 KiB
HTML
<!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>执行一条语句</title>
|
||
</head>
|
||
<body class="docs"><div id="layout">
|
||
<div id="layout-content"><div id="function.oci-execute" class="refentry">
|
||
<div class="refnamediv">
|
||
<h1 class="refname">oci_execute</h1>
|
||
<p class="verinfo">(PHP 5, PHP 7, PECL OCI8 >= 1.1.0)</p><p class="refpurpose"><span class="refname">oci_execute</span> — <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">resource</span> <code class="parameter">$stmt</code></span>
|
||
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$mode</code></span>
|
||
] ) : <span class="type">bool</span></div>
|
||
|
||
<p class="para rdfs-comment">
|
||
<span class="function"><strong>oci_execute()</strong></span> 执行一条之前被解析过的语句(见
|
||
<span class="function"><a href="oci_parse.html" class="function">oci_parse()</a></span>)。可选参数 <code class="parameter">mode</code>
|
||
允许定义执行模式(默认是
|
||
<strong><code>OCI_COMMIT_ON_SUCCESS</code></strong>)。如果不需要将语句自动提交,则需要把
|
||
<code class="parameter">mode</code> 设为 <strong><code>OCI_DEFAULT</code></strong>。
|
||
</p>
|
||
<p class="para">
|
||
用 <strong><code>OCI_DEFAULT</code></strong>
|
||
模式时,将建立一个事务。事务会在关闭连接或脚本结束时(看哪个先)自动回卷。需要明确调用
|
||
<span class="function"><a href="oci_commit.html" class="function">oci_commit()</a></span> 来提交事务,或者
|
||
<span class="function"><a href="oci_rollback.html" class="function">oci_rollback()</a></span> 中止事务。
|
||
</p>
|
||
<p class="para">
|
||
成功时返回 <strong><code>TRUE</code></strong>, 或者在失败时返回 <strong><code>FALSE</code></strong>。
|
||
</p>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
在 PHP 5.0.0 之前的版本必须使用 <span class="function"><a href="ociexecute.html" class="function">ociexecute()</a></span>
|
||
替代本函数。该函数名仍然可用,为向下兼容作为
|
||
<span class="function"><strong>oci_execute()</strong></span> 的别名。不过其已被废弃,不推荐使用。
|
||
</p>
|
||
</p></blockquote>
|
||
</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">
|
||
A valid OCI statement identifier.
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">mode</code></dt>
|
||
|
||
<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_DEFAULT</code></strong></td>
|
||
<td>Obsolete as of PHP 5.3.2 (PECL OCI8 1.4) but still
|
||
available for backward compatibility. Use the
|
||
equivalent <strong><code>OCI_NO_AUTO_COMMIT</code></strong> in new
|
||
code.</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="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="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 an alias
|
||
for <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 a
|
||
transaction. Transactions are automatically rolled back when
|
||
the connection is closed, or when the script ends. Explicitly
|
||
call <span class="function"><a href="oci_commit.html" class="function">oci_commit()</a></span> to commit a transaction,
|
||
or <span class="function"><a href="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="oci_commit.html" class="function">oci_commit()</a></span>
|
||
or <span class="function"><a href="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>
|
||
|
||
|
||
</dl>
|
||
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 returnvalues" id="refsect1-function.oci-execute-returnvalues">
|
||
<h3 class="title">返回值</h3>
|
||
<p class="para">
|
||
成功时返回 <strong><code>TRUE</code></strong>, 或者在失败时返回 <strong><code>FALSE</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-2409">
|
||
<p><strong>Example #1 <span class="function"><strong>oci_execute()</strong></span> for queries</strong></p>
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><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>
|
||
</pre></div>
|
||
</div>
|
||
|
||
</div>
|
||
</p>
|
||
<p class="para">
|
||
<div class="example" id="example-2410">
|
||
<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"><pre><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>
|
||
</pre></div>
|
||
</div>
|
||
|
||
</div>
|
||
</p>
|
||
<p class="para">
|
||
<div class="example" id="example-2411">
|
||
<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"><pre><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>
|
||
</pre></div>
|
||
</div>
|
||
|
||
</div>
|
||
</p>
|
||
<p class="para">
|
||
<div class="example" id="example-2412">
|
||
<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"><pre><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>
|
||
</pre></div>
|
||
</div>
|
||
|
||
</div>
|
||
</p>
|
||
<p class="para">
|
||
<div class="example" id="example-2413">
|
||
<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"><pre><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>
|
||
</pre></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">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="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="oci_parse.html" class="function">oci_parse()</a></span> function does not.
|
||
</p>
|
||
</p></blockquote>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
In PHP versions before 5.0.0 use <span class="function"><a href="ociexecute.html" class="function">ociexecute()</a></span>
|
||
instead. 在当前版本中,旧的函数名还可以被使用,但已经被废弃并不建议使用。
|
||
</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 class="member"><span class="function"><a href="oci_parse.html" class="function" rel="rdfs-seeAlso">oci_parse()</a> - 配置 Oracle 语句预备执行</span></li>
|
||
</ul>
|
||
</p>
|
||
</div>
|
||
|
||
</div></div></div></body></html> |