本类函数使用 Oracle Call Interface(OCI)使用户可以访问 Oracle 10,Oracle9,Oracle8 和 Oracle7 数据库。支持将 PHP 变量与 Oracle 占位符(placeholder)绑定,具有完整的 LOB,FILE 和 ROWID 支持,以及允许使用用户提供的定义变量。
使用本扩展需要 Oracle 客户端库。Windows 用户需要至少 Oracle 8.1 版才能使用 php_oci8.dll。
安装所有所需文件最方便的方法是使用 Oracle Instant Client,可以从此处得到:http://www.oracle.com/technology/tech/oci/instantclient/instantclient.html。Instant Client 不需要 ORACLE_SID 或 ORACLE_HOME 环境变量被设定。不过可能还是要设定 LD_LIBRARY_PATH 和 NLS_LANG。
在使用本扩展之前,请确认已经为 Oracle 用户和 web daemon 用户正确设置了 Oracle 环境变量。这些变量应该在启动 web server 之前设定。下面列出了需要设置的环境变量:
ORACLE_HOME
ORACLE_SID
LD_PRELOAD
LD_LIBRARY_PATH
NLS_LANG
在为 web 服务器用户设置环境变量之后,还需要将 web 服务器用户(nobody,www)加到 oracle 组中。
I如果 web 服务器不能够启动或者在启动的时候崩溃: 检查 Apache 是否连接了 pthread 库:
# ldd /www/apache/bin/httpd libpthread.so.0 => /lib/libpthread.so.0 (0x4001c000) libm.so.6 => /lib/libm.so.6 (0x4002f000) libcrypt.so.1 => /lib/libcrypt.so.1 (0x4004c000) libdl.so.2 => /lib/libdl.so.2 (0x4007a000) libc.so.6 => /lib/libc.so.6 (0x4007e000) /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)如果 libpthread 没有列出,必需重新安装 Apache:
请注意在像 UnixWare 之类的某些操作系统中,使用 libthread 代替了 libpthread。则 PHP 和 Apache 必须使用 EXTRA_LIBS=-lthread 配置。
这些函数的行为受 php.ini 的影响。
表格 1. OCI8 Configuration Options
Name | Default | Changeable | Changelog |
---|---|---|---|
oci8.privileged_connect | "0" | PHP_INI_SYSTEM | Became available in oci8 module version 1.1 |
oci8.max_persistent | "-1" | PHP_INI_SYSTEM | Became available in oci8 module version 1.1 |
oci8.persistent_timeout | "-1" | PHP_INI_SYSTEM | Became available in oci8 module version 1.1 |
oci8.ping_interval | "60" | PHP_INI_SYSTEM | Became available in oci8 module version 1.1 |
oci8.statement_cache_size | "20" | PHP_INI_SYSTEM | Became available in oci8 module version 1.1 |
oci8.default_prefetch | "10" | PHP_INI_SYSTEM | Became available in oci8 module version 1.1 |
oci8.old_oci_close_semantics | "0" | PHP_INI_SYSTEM | Became available in oci8 module version 1.1 |
以下是配置选项的简要解释。
This option enables privileged connections using external credentials (OCI_SYSOPER, OCI_SYSDBA).
The maximum number of persistent OCI8 connections per process. Setting this option to -1 means that there is no limit.
The maximum length of time (in seconds) that a given process is allowed to maintain an idle persistent connection. Setting this option to -1 means that idle persistent connections will be maintained forever.
The length of time (in seconds) that must pass before issuing a ping during oci_pconnect(). When set to 0, persistent connections will be pinged every time they are reused. To disable pings completely, set this option to -1.
注: Disabling pings will cause oci_pconnect() calls to operate at the highest efficiency, but may cause PHP to not detect faulty connections, such as those caused by network partitions, or if the Oracle server has gone down since PHP connected, until later in the script. Consult the oci_pconnect() documentation for more information.
This option enables statement caching, and specifies how many statements to cache. To disable statement caching just set this option to 0.
注: A larger cache can result in improved performance, at the cost of increased memory usage.
This option enables statement prefetching and sets the default number of rows that will be fetched automatically after statement execution.
注: A larger prefetch can result in improved performance, at the cost of increased memory usage.
This option controls oci_close() behaviour. Enabling it means that oci_close() will do nothing; the connection will not be closed until the end of the script. This is for backward compatibility only. If you find that you need to enable this setting, you are strongly encouraged to remove the oci_close() calls from your application instead of enabling this option.
以下常量由本扩展模块定义,因此只有在本扩展模块被编译到 PHP 中,或者在运行时被动态加载后才有效。
Statement execution mode. Statement is not committed automatically when using this mode.
Statement execution mode. Use this mode if you don't want to execute the query, but get the select-list's description.
Statement execution mode. Statement is automatically committed after oci_execute() call.
Statement fetch mode. Used when the application knows in advance exactly how many rows it will be fetching. This mode turns prefetching off for Oracle release 8 or later mode. Cursor is cancelled after the desired rows are fetched and may result in reduced server-side resource usage.
Used with oci_bind_by_name() when binding BFILEs.
Used with oci_bind_by_name() when binding CFILEs.
Used with oci_bind_by_name() when binding CLOBs.
Used with oci_bind_by_name() when binding BLOBs.
Used with oci_bind_by_name() when binding ROWIDs.
Used with oci_bind_by_name() when binding cursors, previously allocated with oci_new_descriptor().
Used with oci_bind_by_name() when binding named data types. Note: in PHP < 5.0 it was called OCI_B_SQLT_NTY.
The same as OCI_B_BFILE.
The same as OCI_B_CFILEE.
The same as OCI_B_CLOB.
The same as OCI_B_BLOB.
The same as OCI_B_ROWID.
The same as OCI_B_NTY.
Default mode of oci_fetch_all().
Alternative mode of oci_fetch_all().
Used with oci_fetch_all() and oci_fetch_array() to get an associative array as a result.
Used with oci_fetch_all() and oci_fetch_array() to get an enumerated array as a result.
Used with oci_fetch_all() and oci_fetch_array() to get an array with both associative and number indices.
Used with oci_fetch_array() to get empty array elements if field's value is NULL.
Used with oci_fetch_array() to get value of LOB instead of the descriptor.
This flag tells oci_new_descriptor() to initialize new FILE descriptor.
This flag tells oci_new_descriptor() to initialize new LOB descriptor.
This flag tells oci_new_descriptor() to initialize new ROWID descriptor.
The same as OCI_DTYPE_FILE.
The same as OCI_DTYPE_LOB.
The same as OCI_DTYPE_ROWID.
Used with oci_connect() to connect as SYSOPER using external credentials (oci8.privileged_connect should be enabled for this).
Used with oci_connect() to connect as SYSDBA using external credentials (oci8.privileged_connect should be enabled for this).
Used with OCI-Lob->flush to free buffers used.
Used with OCI-Lob->writeTemporary to indicate explicilty that temporary CLOB should be created.
Used with OCI-Lob->writeTemporary to indicate explicilty that temporary BLOB should be created.
例子 2. 用绑定变量插入
|
例子 3. 将数据插入到 CLOB 列中
|
可以很容易地访问存储过程,就和从命令行访问一样。
例子 4. 使用存储过程
|
OCI8 扩展提供了 3 个不同函数来连接 Oracle。取决于用户来使用对自己的应用程序最合适的函数。本节的信息有助于用户作出合适的选择。
连接到 Oracle 服务器从所需的时间上来讲是个相当花费的操作。oci_pconnect() 函数使用了一个连接的持久缓冲区,可以被不同的脚本请求重复使用。这意味着通常在每个 PHP 进程(或 Apache 子进程)中只需要连接一次。
如果应用程序连接 Oracle 时对每个 web 用户都使用了不同的认证信息,则由 oci_pconnect() 使用的持久缓冲区就用处不大了,因为随着并发用户的增加,到某个程度后会由于要保持太多的空闲连接而对 Oracle 服务器的整体性能起到逆反的影响。如果应用程序是这样的架构,建议要么用 oci8.max_persistent 和 oci8.persistent_timeout 配置选项(此二者可以使用户控制持久连接缓冲区的大小和生命周期)来协调应用程序,要么用 oci_connect() 来连接。
oci_connect() 和 oci_pconnect() 都使用了一个连接缓冲区。如果在某个脚本中用同样的参数多次调用 oci_connect(),则第二个和之后的调用会返回已有的连接句柄。oci_connect() 使用的连接缓冲区会在脚本执行完毕后或者明确地关闭了连接句柄时被清空。oci_pconnect() 有相似的行为,不过其缓冲区独立地维持着并在不同请求之间都存活着。
要记住此缓冲特性,因为它使两个句柄没有在事务级隔离开来(事实上是同一个连接句柄,因此没有任何方式的隔离)。如果应用程序需要两个独立的,事务级隔离的连接,应该使用 oci_new_connect()。
oci_new_connect() 总是创建一个到 Oracle 服务器的新连接,不管其它连接是否已经存在。高流量的 web 应用应该避免使用 oci_new_connect(),尤其是在程序最忙的部分。
表格 2. 用 oci_bind_by_name() 函数绑定参数时支持以下类型
类型 | 映射 |
---|---|
SQLT_NTY | 映射到一个来自 PHP collection 对象的本地 collection 类型,例如由 oci_new_collection() 创建的。 |
SQLT_BFILEE | 映射到一个本地的 descriptor,例如由 oci_new_descriptor() 创建的。 |
SQLT_CFILEE | 映射到一个本地的 descriptor,例如由 oci_new_descriptor() 创建的。 |
SQLT_CLOB | 映射到一个本地的 descriptor,例如由 oci_new_descriptor() 创建的。 |
SQLT_BLOB | 映射到一个本地的 descriptor,例如由 oci_new_descriptor() 创建的。 |
SQLT_RDD | 映射到一个本地的 descriptor,例如由 oci_new_descriptor() 创建的。 |
SQLT_NUM | 将 PHP 参数转换为 C 语言的 long 类型,并绑定为其值。 |
SQLT_RSET | 映射到一个本地的 statement 句柄,例如由 oci_parse() 创建或从其它 OCI 查询取得的。 |
SQLT_CHR 以及任何其它类型 | 将 PHP 参数转换为字符串类型并绑定为字符串。 |