oci_pconnect

(PHP 5, PECL OCI8 >= 1.1.0)

oci_pconnectConnect to an Oracle database using a persistent connection

說明

resource oci_pconnect ( string $username , string $password [, string $connection_string [, string $character_set [, int $session_mode ]]] )

Creates a persistent connection to an Oracle server and logs on.

Persistent connections are cached and re-used between requests, resulting in reduced overhead on each page load; a typical PHP application will have a single persistent connection open against an Oracle server per Apache child process (or PHP FastCGI/CGI process). See the Persistent Database Connections section for more information.

參數

username

The Oracle user name.

password

The password for username.

connection_string

包含要連接的 Oracle 實例。可以是 » Easy Connect 字串,或是 tnsnames.ora 文件中的連接名,或是本地 Oracle 實體名。

如果不指定,PHP 使用環境變數來確定連接的 Oracle 實例,諸如 TWO_TASK(Linux 下)或 LOCAL(Windows 下)與 ORACLE_SID 等。

要使用 Easy Connect 命名方法,PHP 必須與 Oracle 10g 或更高版本的客戶端函式庫進行連結。Oracle 10g 的 Easy Connect 字串格式:[//]host_name[:port][/service_name]。Oracle 11g 則為:[//]host_name[:port][/service_name][:server_type][/instance_name]。服務名可在資料庫伺服器機器上執行 Oracle 工具程式 lsnrctl status 找到。

tnsnames.ora 檔案可在 Oracle Net 尋找路徑中,此路徑包括 $ORACLE_HOME/network/admin/etc。 另一種方法是設定 TNS_ADMIN 以便通過 $TNS_ADMIN/tnsnames.ora 來讀取。表確認 web 背景程序可讀取此文件。

character_set

使用 Oracle 客戶端函式庫來確定字元集。字元集不需要與資料庫的字符集相匹配。如果不匹配,Oracle 會儘可能地將資料從資料庫字元集進行轉換。因為依賴於字元集,可能不能給出可用的結果。轉換也增加一些時間。

如果不指定,Oracle 客戶端用 NLS_LANG 環境變數來決定字元集。

傳遞此參數可減少連接時間。

session_mode

此參數在 PHP 5(PECL OCI8 1.1)版本開始可用,並接受下列值:OCI_DEFAULTOCI_SYSOPEROCI_SYSDBA。如為 OCI_SYSOPEROCI_SYSDBA 其中之一,此函式將會使用外部的憑證建立有特權的連結。有特權的連結預設是禁用的。需要將 oci8.privileged_connect 設為 On 來啟用。

PHP 5.3(PECL OCI8 1.3.4)引進了 OCI_CRED_EXT 模式值。使用外部或操作系統認證必需在 Oracle 資料庫中進行設定。OCI_CRED_EXT 標誌只可用於用戶為 "/",密碼為空的情況。oci8.privileged_connect 可為 OnOff

OCI_CRED_EXT 可與 OCI_SYSOPEROCI_SYSDBA 模式組合使用。

OCI_CRED_EXT 由於安全的原因不支援 Windows 系統。

回傳值

Returns a connection identifier or FALSE on error.

範例

Example #1 Basic oci_pconnect() Example using Easy Connect syntax

<?php

// Connects to the XE service (i.e. database) on the "localhost" machine
$conn oci_pconnect('hr''welcome''localhost/XE');
if (!
$conn) {
    
$e oci_error();
    
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

$stid oci_parse($conn'SELECT * FROM employees');
oci_execute($stid);

echo 
"<table border='1'>\n";
while (
$row oci_fetch_array($stidOCI_ASSOC+OCI_RETURN_NULLS)) {
    echo 
"<tr>\n";
    foreach (
$row as $item) {
        echo 
"    <td>" . ($item !== null htmlentities($itemENT_QUOTES) : "&nbsp;") . "</td>\n";
    }
    echo 
"</tr>\n";
}
echo 
"</table>\n";

?>

See oci_connect() for further examples of parameter usage.

註釋

Note: Starting with PHP 5.1.2 and PECL OCI8 1.1, the lifetime and maximum number of persistent Oracle connections per PHP process can be tuned by setting the following configuration values: oci8.persistent_timeout, oci8.ping_interval and oci8.max_persistent.

參見

add a note add a note

User Contributed Notes 2 notes

up
6
php at jaggard dot org dot uk
15 years ago
[Editor's note: OCI8 1.3 should not experience the problem described in this user comment. The first use of such a connection will return an Oracle error which will trigger a cleanup in PHP.  Subsequent persistent connection calls will then succeed.  For high availability you might consider doing consecutive oci_pconnect calls in your script.]

If you connect using oci_pconnect and the connection has logged you off but is still valid, there seems to be no way to re-use that connection. The next time I try oci_pconnect and then perform an oci_execute operation, I get a "ORA-01012: not logged on" warning. This problem remains, even if I close the connection using oci_close. I ended up with the following (rather annoying) code.

<?php
   
function getOracleConnection()
    {
      if (!
function_exists('oci_pconnect'))
        return
false;
     
$toReturn = oci_pconnect('user', 'pass', 'db');
      if (
$testRes = @oci_parse($toReturn, 'SELECT Count(group_type_code) FROM pvo.group_type'))
        if (@
oci_execute($testRes))
          if (@
oci_fetch_array($testRes))
            return
$toReturn;
     
oci_close($toReturn);
      if (!
function_exists('oci_connect'))
        return
false;
     
$toReturn = oci_connect('user', 'pass', 'db');
      if (
$testRes = @oci_parse($toReturn, 'SELECT Count(group_type_code) FROM pvo.group_type'))
        if (@
oci_execute($testRes))
          if (@
oci_fetch_array($testRes))
            return
$toReturn;
     
oci_close($toReturn);
      if (!
function_exists('oci_new_connect'))
        return
false;
     
$toReturn = oci_new_connect('user', 'pass', 'db');
      if (
$testRes = @oci_parse($toReturn, 'SELECT Count(group_type_code) FROM pvo.group_type'))
        if (@
oci_execute($testRes))
          if (@
oci_fetch_array($testRes))
            return
$toReturn;
     
oci_close($toReturn);
      return
false;
    }
?>
up
1
gotankersley at NOSPAM dot com
11 years ago
Installed on CentOS 6.2, and had lots of trouble getting it to recognize tnsnames.ora.  The fix for me was:

1. Make sure apache is getting the TNS_ADMIN env variable by putting it in the /etc/init.d/httpd file:
TNS_ADMIN=/usr/lib/oracle/11.2/client64/network/admin
export PATH TNS_ADMIN

This can be debugging in PHP by <?php echo system('env'); ?> and by verifying that TNS_ADMIN is there.

2. Make sure to use the name at the beginning of the tnsnames.ora file - not the SID (although ideally they should match.  However, if the name at the beginning is XXXX.world then pconnect will expect this - not the SID)
To Top