©
本文檔使用 php中文網手册 發布
(PHP 5, PECL OCI8 >= 1.1.0)
oci_execute — 执行一条语句
$stmt
[, int $mode
] ) oci_execute() 执行一条之前被解析过的语句(见
oci_parse() )。可选参数 mode
允许定义执行模式(默认是
OCI_COMMIT_ON_SUCCESS
)。如果不需要将语句自动提交,则需要把
mode
设为 OCI_DEFAULT
。
用 OCI_DEFAULT
模式时,将建立一个事务。事务会在关闭连接或脚本结束时(看哪个先)自动回卷。需要明确调用
oci_commit() 来提交事务,或者
oci_rollback() 中止事务。
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
Note:
在 PHP 5.0.0 之前的版本必须使用 ociexecute() 替代本函数。该函数名仍然可用,为向下兼容作为 oci_execute() 的别名。不过其已被废弃,不推荐使用。
statement
A valid OCI statement identifier.
mode
An optional second parameter can be one of the following constants:
Constant | Description |
---|---|
OCI_COMMIT_ON_SUCCESS | Automatically commit all outstanding changes for this connection when the statement has succeeded. This is the default. |
OCI_DEFAULT | Obsolete as of PHP 5.3.2 (PECL OCI8 1.4) but still
available for backward compatibility. Use the
equivalent OCI_NO_AUTO_COMMIT in new
code. |
OCI_DESCRIBE_ONLY | Make query meta data available to functions like oci_field_name() but do not create a result set. Any subsequent fetch call such as oci_fetch_array() will fail. |
OCI_NO_AUTO_COMMIT | Do not automatically commit changes. Prior to PHP
5.3.2 (PECL OCI8 1.4)
use OCI_DEFAULT which is an alias
for OCI_NO_AUTO_COMMIT . |
Using OCI_NO_AUTO_COMMIT
mode starts a
transaction. Transactions are automatically rolled back when
the connection is closed, or when the script ends. Explicitly
call oci_commit() to commit a transaction,
or oci_rollback() to abort it.
When inserting or updating data, using transactions is recommended for relational data consistency and for performance reasons.
If OCI_NO_AUTO_COMMIT
mode is used for any
statement including queries, and
oci_commit()
or oci_rollback() 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 OCI_NO_AUTO_COMMIT
mode for queries or
PL/SQL. Be careful to ensure the appropriate transactional
consistency for the application when
using oci_execute() with different modes in
the same script.
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
Example #1 oci_execute() for queries
<?php
$conn = oci_connect ( 'hr' , 'welcome' , 'localhost/XE' );
$stid = oci_parse ( $conn , 'SELECT * FROM employees' );
oci_execute ( $stid );
echo "<table border='1'>\n" ;
while ( $row = oci_fetch_array ( $stid , OCI_ASSOC + OCI_RETURN_NULLS )) {
echo "<tr>\n" ;
foreach ( $row as $item ) {
echo " <td>" . ( $item !== null ? htmlentities ( $item , ENT_QUOTES ) : " " ) . "</td>\n" ;
}
echo "</tr>\n" ;
}
echo "</table>\n" ;
?>
Example #2 oci_execute() without specifying a mode example
<?php
// Before running, create the table:
// CREATE TABLE MYTABLE (col1 NUMBER);
$conn = oci_connect ( 'hr' , 'welcome' , 'localhost/XE' );
$stid = oci_parse ( $conn , 'INSERT INTO mytab (col1) VALUES (123)' );
oci_execute ( $stid ); // The row is committed and immediately visible to other users
?>
Example #3 oci_execute() with OCI_NO_AUTO_COMMIT
example
<?php
// Before running, create the table:
// CREATE TABLE MYTABLE (col1 NUMBER);
$conn = oci_connect ( 'hr' , 'welcome' , 'localhost/XE' );
$stid = oci_parse ( $conn , 'INSERT INTO mytab (col1) VALUES (:bv)' );
oci_bind_by_name ( $stid , ':bv' , $i , 10 );
for ( $i = 1 ; $i <= 5 ; ++ $i ) {
oci_execute ( $stid , OCI_NO_AUTO_COMMIT ); // use OCI_DEFAULT for PHP <= 5.3.1
}
oci_commit ( $conn ); // commits all new values: 1, 2, 3, 4, 5
?>
Example #4 oci_execute() with different commit modes example
<?php
// Before running, create the table:
// CREATE TABLE MYTABLE (col1 NUMBER);
$conn = oci_connect ( 'hr' , 'welcome' , 'localhost/XE' );
$stid = oci_parse ( $conn , 'INSERT INTO mytab (col1) VALUES (123)' );
oci_execute ( $stid , OCI_NO_AUTO_COMMIT ); // data not committed
$stid = oci_parse ( $conn , 'INSERT INTO mytab (col1) VALUES (456)' );
oci_execute ( $stid ); // commits both 123 and 456 values
?>
Example #5 oci_execute() with
OCI_DESCRIBE_ONLY
example
<?php
$conn = oci_connect ( 'hr' , 'welcome' , 'localhost/XE' );
$stid = oci_parse ( $conn , 'SELECT * FROM locations' );
oci_execute ( $s , OCI_DESCRIBE_ONLY );
for ( $i = 1 ; $i <= oci_num_fields ( $stid ); ++ $i ) {
echo oci_field_name ( $stid , $i ) . "<br>\n" ;
}
?>
Note:
Transactions are automatically rolled back when connections are closed, or when the script ends, whichever is soonest. Explicitly call oci_commit() to commit a transaction.
Any call to oci_execute() that uses
OCI_COMMIT_ON_SUCCESS
mode explicitly or by default will commit any previous uncommitted transaction.Any Oracle DDL statement such as CREATE or DROP will automatically commit any uncommitted transaction.
Note:
Because the oci_execute() function generally sends the statement to the database, oci_execute() can identify some statement syntax errors that the lightweight, local oci_parse() function does not.
Note:
In PHP versions before 5.0.0 use ociexecute() instead. 在当前版本中,旧的函数名还可以被使用,但已经被废弃并不建议使用。
[#1] michaelhughes at strath dot ac dot uk [2015-11-11 11:38:15]
You can pass a "false" value to oci_execute() and this returns a null value, instead of the documented false value.
<?php
$conn = oci_connect('username', 'password, '//hostname:1521/DB');
$result = oci_execute(false);
var_dump($result);
?>
Results in "null", so performing an Identical test:
<?php
if ($results === false) {
//throw exception
}
?>
won't trap a problem, where as the Equal test (==) would:
<?php
if ($results == false) {
//throw exception
}
?>
So testing the result of a statement like oci_parse() is important!
[#2] filipesampaio at hotmail dot com [2011-04-05 05:21:43]
Just to write it down. I was trying to do a simple SELECT on a Cach?? (http://www.intersystems.com/cache/) table through an Oracle dblink, but always received the error "ORA-01002: fetch out of sequence". The solution was using OCI_NO_AUTO_COMMIT on the oci_execute function.
[#3] tower98 at gmail dot com [2010-08-25 01:35:47]
Notice (PHP 5.2.12-pl0-gentoo):
You can parse empty query, you can execute empty query (returns true), but you cannot fetch data from empty query. So, if you provide query as variable, make sure it isn't empty.
<?php
$q = oci_parse($c, "");
if($q != false){
// parsing empty query != false
if(oci_execute($q){
// executing empty query != false
if(oci_fetch_all($q, $data, 0, -1, OCI_FETCHSTATEMENT_BY_ROW) == false){
// but fetching executed empty query results in error (ORA-24338: statement handle not executed)
$e = oci_error($q);
echo $e['message'];
}
}
else{
$e = oci_error($q);
echo $e['message'];
}
}
else{
$e = oci_error($link);
echo $e['message'];
}
?>