oci_connect

(PHP 5, PHP 7, PHP 8, PECL OCI8 >= 1.1.0)

oci_connect建立一個到 Oracle 服務(wù)器的連接

說明

oci_connect(
    string $username,
    string $password,
    string $db = ?,
    string $charset = ?,
    int $session_mode = ?
): resource

oci_connect() 返回一個大多數(shù) OCI 調(diào)用都需要的連接標(biāo)識符。可選的第三個參數(shù)可以包含想要連接的本地 Oracle 的實例的或者在 tnsnames.ora 中的條目的名字。如果沒有指定可選的第三個參數(shù),PHP 使用環(huán)境變量 ORACLE_SID(Oracle 實例)或 TWO_TASKtnsnames.ora)來確定連接哪一個數(shù)據(jù)庫。

session_mode 參數(shù)自版本 1.1 起可用并接受如下值:OCI_DEFAULTOCI_SYSOPEROCI_SYSDBA。如果指定了 OCI_SYSOPEROCI_SYSDBA 其中之一,oci_connect() 將嘗試使用外部認證信息建立特權(quán)連接。特權(quán)連接默認被禁止。要啟用,需要將 oci8.privileged_connect 設(shè)為 On。

注意: oci_connect() 用同樣的參數(shù)第二次及之后的調(diào)用將返回第一次調(diào)用所返回的連接句柄。也就是說對一個句柄發(fā)出的查詢也適用于另一個句柄,因為它們是同一個句柄。此行為演示于下面的例 1。如果需要兩個句柄在事務(wù)上互相隔離開,應(yīng)該使用 oci_new_connect() 來替代。

使用 Oracle 客戶端庫來確定字符集。字符集不需要與數(shù)據(jù)庫的字符集相匹配。如果不匹配,Oracle 會盡可能地將數(shù)據(jù)從數(shù)據(jù)庫字符集進行轉(zhuǎn)換。因為依賴于字符集,可能不能給出可用的結(jié)果。轉(zhuǎn)換也增加一些時間開銷。

如果不指定,Oracle 客戶端用 NLS_LANG 環(huán)境變量來決定字符集。

傳遞此參數(shù)可減少連接時間。

示例 #1 oci_connect() 例子

<?php
echo "<pre>";
$db "";

$c1 oci_connect("scott""tiger"$db);
$c2 oci_connect("scott""tiger"$db);

function 
create_table($conn)
{
  
$stmt oci_parse($conn"create table scott.hallo (test varchar2(64))");
  
oci_execute($stmt);
  echo 
$conn " created table\n\n";
}

function 
drop_table($conn)
{
  
$stmt oci_parse($conn"drop table scott.hallo");
  
oci_execute($stmt);
  echo 
$conn " dropped table\n\n";
}

function 
insert_data($conn)
{
  
$stmt oci_parse($conn"insert into scott.hallo
            values('
$conn' || ' ' || to_char(sysdate,'DD-MON-YY HH24:MI:SS'))");
  
oci_execute($stmtOCI_DEFAULT);
  echo 
$conn " inserted hallo\n\n";
}

function 
delete_data($conn)
{
  
$stmt oci_parse($conn"delete from scott.hallo");
  
oci_execute($stmtOCI_DEFAULT);
  echo 
$conn " deleted hallo\n\n";
}

function 
commit($conn)
{
  
oci_commit($conn);
  echo 
$conn " committed\n\n";
}

function 
rollback($conn)
{
  
oci_rollback($conn);
  echo 
$conn " rollback\n\n";
}

function 
select_data($conn)
{
  
$stmt oci_parse($conn"select * from scott.hallo");
  
oci_execute($stmtOCI_DEFAULT);
  echo 
$conn."----selecting\n\n";
  while (
oci_fetch($stmt)) {
    echo 
$conn " [" oci_result($stmt"TEST") . "]\n\n";
  }
  echo 
$conn "----done\n\n";
}

create_table($c1);
insert_data($c1);   // Insert a row using c1
insert_data($c2);   // Insert a row using c2

select_data($c1);   // Results of both inserts are returned
select_data($c2);

rollback($c1);      // Rollback using c1

select_data($c1);   // Both inserts have been rolled back
select_data($c2);

insert_data($c2);   // Insert a row using c2
commit($c2);        // Commit using c2

select_data($c1);   // Result of c2 insert is returned

delete_data($c1);   // Delete all rows in table using c1
select_data($c1);   // No rows returned
select_data($c2);   // No rows returned
commit($c1);        // Commit using c1

select_data($c1);   // No rows returned
select_data($c2);   // No rows returned

drop_table($c1);
echo 
"</pre>";
?>

oci_connect() 如果出錯則返回 false

注意:

在 PHP 5.0.0 之前的版本必須使用 ocilogon() 替代本函數(shù)。該函數(shù)名仍然可用,為向下兼容作為 oci_connect() 的別名。不過其已被廢棄,不推薦使用。

參見 oci_pconnect(),oci_new_connect()oci_close()

參數(shù)

username

The Oracle user name.

password

The password for username.

connection_string

包含要連接的 Oracle 實例??梢允?? Easy Connect 串,或是 tnsnames.ora 文件中的連接名,或是本地 Oracle 實例名。

如果不指定或者為 null,PHP 使用環(huán)境變量來確定連接的 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 19c 加入類更多選項,例如 timeout 和 keep-alive 設(shè)置。 請參考 Oracle 文檔。 服務(wù)名可在數(shù)據(jù)庫服務(wù)器機器上運行 Oracle 實用程序 lsnrctl status 找到。

tnsnames.ora 文件可在 Oracle Net 查找路徑中,此路徑包括 /your/path/to/instantclient/network/admin、$ORACLE_HOME/network/admin 、/etc。 另一種方法是設(shè)置 TNS_ADMIN 以便通過 $TNS_ADMIN/tnsnames.ora 來讀取。表確認 web 守護進程可讀取此文件。

character_set

使用 Oracle 客戶端庫來確定字符集。字符集不需要與數(shù)據(jù)庫的字符集相匹配。如果不匹配,Oracle 會盡可能地將數(shù)據(jù)從數(shù)據(jù)庫字符集進行轉(zhuǎn)換。因為依賴于字符集,可能不能給出可用的結(jié)果。轉(zhuǎn)換也增加一些時間開銷。

如果不指定,Oracle 客戶端用 NLS_LANG 環(huán)境變量來決定字符集。

傳遞此參數(shù)可減少連接時間。

session_mode

此參數(shù)在 PHP 5(PECL OCI8 1.1)版本開始可用,并收受下列值:OCI_DEFAULT,OCI_SYSOPEROCI_SYSDBA。如為 OCI_SYSOPEROCI_SYSDBA 其中之一,此函數(shù)將會使用外部的證書建立有特權(quán)的連接。有特權(quán)的連接默認是禁用的。需要將 oci8.privileged_connect 設(shè)為 On 來啟用。

PHP 5.3(PECL OCI8 1.3.4)引進了 OCI_CRED_EXT 模式值。使用外部或操作系統(tǒng)認證必需在 Oracle 數(shù)據(jù)庫中進行配置。OCI_CRED_EXT 標(biāo)志只可用于用戶為 "/",密碼為空的情況。oci8.privileged_connect 可為 OnOff。

OCI_CRED_EXT 可與 OCI_SYSOPEROCI_SYSDBA 模式組合使用。

OCI_CRED_EXT 由于安全的原因不支持 Windows 系統(tǒng)。

返回值

Returns a connection identifier or false on error.

范例

示例 #2 Basic oci_connect() using Easy Connect syntax

<?php

// Connects to the XE service (i.e. database) on the "localhost" machine
$conn oci_connect('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";

?>

示例 #3 Basic oci_connect() using a Network Connect name

<?php

// Connects to the MYDB database described in tnsnames.ora file,
// One example tnsnames.ora entry for MYDB could be:
//   MYDB =
//     (DESCRIPTION =
//       (ADDRESS = (PROTOCOL = TCP)(HOST = mymachine.oracle.com)(PORT = 1521))
//       (CONNECT_DATA =
//         (SERVER = DEDICATED)
//         (SERVICE_NAME = XE)
//       )
//     )

$conn oci_connect('hr''welcome''MYDB');
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";

?>

示例 #4 oci_connect() with an explicit character set

<?php

$conn 
oci_connect('hr''welcome''localhost/XE''AL32UTF8');
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";

?>

示例 #5 Using multiple calls to oci_connect()

<?php

$c1 
oci_connect("hr""welcome"'localhost/XE');
$c2 oci_connect("hr""welcome"'localhost/XE');

// Both $c1 and $c2 show the same PHP resource id meaning they use the
// same underlying database connection
echo "c1 is $c1<br>\n";
echo 
"c2 is $c2<br>\n";

function 
create_table($conn)
{
    
$stmt oci_parse($conn"create table hallo (test varchar2(64))");
    
oci_execute($stmt);
    echo 
"Created table<br>\n";
}

function 
drop_table($conn)
{
    
$stmt oci_parse($conn"drop table hallo");
    
oci_execute($stmt);
    echo 
"Dropped table<br>\n";
}

function 
insert_data($connname$conn)
{
    
$stmt oci_parse($conn"insert into hallo
              values(to_char(sysdate,'DD-MON-YY HH24:MI:SS'))"
);
    
oci_execute($stmtOCI_DEFAULT);
    echo 
"$connname inserted row without committing<br>\n";
}

function 
rollback($connname$conn)
{
    
oci_rollback($conn);
    echo 
"$connname rollback<br>\n";
}

function 
select_data($connname$conn)
{
    
$stmt oci_parse($conn"select * from hallo");
    
oci_execute($stmtOCI_DEFAULT);
    echo 
"$connname ----selecting<br>\n";
    while (
oci_fetch($stmt)) {
        echo 
"    " oci_result($stmt"TEST") . "<br>\n";
    }
    echo 
"$connname ----done<br>\n";
}

create_table($c1);

insert_data('c1'$c1);   // Insert a row using c1
sleep(2);                 // sleep to show a different timestamp for the 2nd row
insert_data('c2'$c2);   // Insert a row using c2

select_data('c1'$c1);   // Results of both inserts are returned
select_data('c2'$c2);   // Results of both inserts are returned

rollback('c1'$c1);      // Rollback using c1

select_data('c1'$c1);   // Both inserts have been rolled back
select_data('c2'$c2);

drop_table($c1);

// Closing one of the connections makes the PHP variable unusable, but
// the other could be used
oci_close($c1);
echo 
"c1 is $c1<br>\n";
echo 
"c2 is $c2<br>\n";


// Output is:
//    c1 is Resource id #5
//    c2 is Resource id #5
//    Created table
//    c1 inserted row without committing
//    c2 inserted row without committing
//    c1 ----selecting
//        09-DEC-09 12:14:43
//        09-DEC-09 12:14:45
//    c1 ----done
//    c2 ----selecting
//        09-DEC-09 12:14:43
//        09-DEC-09 12:14:45
//    c2 ----done
//    c1 rollback
//    c1 ----selecting
//    c1 ----done
//    c2 ----selecting
//    c2 ----done
//    Dropped table
//    c1 is 
//    c2 is Resource id #5

?>

注釋

注意:

An incorrectly installed or configured OCI8 extension will often manifest itself as a connection problem or error. See Installing/Configuring for troubleshooting information.

注意:

In PHP versions before 5.0.0 use ocilogon() instead.

參見