oci_connect

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

oci_connect锟斤拷锟斤拷一锟斤拷锟斤拷 Oracle 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷

说锟斤拷

oci_connect ( string $username , string $password [, string $db [, string $charset [, int $session_mode ]]] ) : resource

oci_connect() 锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟� OCI 锟斤拷锟矫讹拷锟斤拷要锟斤拷锟斤拷锟接憋拷识锟斤拷锟斤拷锟斤拷选锟侥碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟皆帮拷锟斤拷锟斤拷要锟斤拷锟接的憋拷锟斤拷 Oracle 锟斤拷实锟斤拷锟侥伙拷锟斤拷锟斤拷 tnsnames.ora 锟叫碉拷锟斤拷目锟斤拷锟斤拷锟街★拷锟斤拷锟矫伙拷锟街革拷锟斤拷锟窖★拷牡锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟絇HP 使锟矫伙拷锟斤拷锟斤拷锟斤拷 ORACLE_SID锟斤拷Oracle 实锟斤拷锟斤拷锟斤拷 TWO_TASK锟斤拷tnsnames.ora锟斤拷锟斤拷确锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟捷库。

session_mode 锟斤拷锟斤拷锟皆版本 1.1 锟斤拷锟斤拷貌锟斤拷锟斤拷锟斤拷锟斤拷锟街碉拷锟�OCI_DEFAULT锟斤拷OCI_SYSOPER 锟斤拷 OCI_SYSDBA锟斤拷锟斤拷锟街革拷锟斤拷锟� OCI_SYSOPER 锟斤拷 OCI_SYSDBA 锟斤拷锟斤拷之一锟斤拷oci_connect() 锟斤拷锟斤拷锟斤拷使锟斤拷锟解部锟斤拷证锟斤拷息锟斤拷锟斤拷锟斤拷权锟斤拷锟接★拷锟斤拷权锟斤拷锟斤拷默锟较憋拷锟斤拷止锟斤拷要锟斤拷锟矫o拷锟斤拷要锟斤拷 oci8.privileged_connect 锟斤拷为 On锟斤拷

Note: 锟斤拷 oci_connect() 锟斤拷同锟斤拷锟侥诧拷锟斤拷锟节讹拷锟轿硷拷之锟斤拷牡锟斤拷媒锟斤拷锟斤拷氐锟揭伙拷蔚锟斤拷锟斤拷锟斤拷锟斤拷氐锟斤拷锟斤拷泳锟斤拷锟斤拷也锟斤拷锟斤拷说锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷牟锟窖诧拷锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟斤拷为锟斤拷锟斤拷锟斤拷同一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟轿拷锟绞撅拷锟斤拷锟斤拷锟斤拷锟斤拷 1锟斤拷锟斤拷锟斤拷锟揭拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟较伙拷锟斤拷锟斤拷肟拷锟接︼拷锟绞癸拷锟� oci_new_connect() 锟斤拷锟斤拷锟斤拷锟�

使锟斤拷 Oracle 锟酵伙拷锟剿匡拷锟斤拷确锟斤拷锟街凤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷要锟斤拷锟斤拷锟捷匡拷锟斤拷址锟斤拷锟斤拷锟狡ワ拷洹o拷锟斤拷锟斤拷匹锟戒,Oracle 锟结尽锟斤拷锟杰地斤拷锟斤拷锟捷达拷锟斤拷锟捷匡拷锟街凤拷锟斤拷锟斤拷锟斤拷转锟斤拷锟斤拷锟斤拷为锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟杰诧拷锟杰革拷锟斤拷锟斤拷锟矫的斤拷锟斤拷锟阶拷锟揭诧拷锟斤拷锟揭恍┦憋拷淇拷锟斤拷锟�

锟斤拷锟斤拷锟街革拷锟斤拷锟絆racle 锟酵伙拷锟斤拷锟斤拷 NLS_LANG 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷

锟斤拷锟捷此诧拷锟斤拷锟缴硷拷锟斤拷锟斤拷锟斤拷时锟戒。

Example #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锟斤拷

Note:

锟斤拷 PHP 5.0.0 之前锟侥版本锟斤拷锟斤拷使锟斤拷 ocilogon() 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷煤锟斤拷锟斤拷锟斤拷锟饺伙拷锟斤拷茫锟轿拷锟斤拷录锟斤拷锟斤拷锟轿� oci_connect() 锟侥憋拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟窖憋拷锟斤拷锟斤拷锟斤拷锟斤拷锟狡硷拷使锟矫★拷

锟轿硷拷 oci_pconnect()锟斤拷oci_new_connect() 锟斤拷 oci_close()锟斤拷

锟斤拷锟斤拷

username

The Oracle user name.

password

The password for username.

connection_string

锟斤拷锟斤拷要锟斤拷锟接碉拷 Oracle 实锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 » Easy Connect 锟斤拷锟斤拷锟斤拷锟斤拷 tnsnames.ora 锟侥硷拷锟叫碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟角憋拷锟斤拷 Oracle 实锟斤拷锟斤拷锟斤拷

锟斤拷锟斤拷锟街革拷锟斤拷锟絇HP 使锟矫伙拷锟斤拷锟斤拷锟斤拷锟斤拷确锟斤拷锟斤拷锟接碉拷 Oracle 实锟斤拷锟斤拷锟斤拷锟斤拷 TWO_TASK锟斤拷Linux 锟铰o拷锟斤拷 LOCAL锟斤拷Windows 锟铰o拷锟斤拷 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 锟斤拷锟斤拷路锟斤拷锟叫o拷锟斤拷路锟斤拷锟斤拷锟斤拷 $ORACLE_HOME/network/admin 锟斤拷 /etc锟斤拷 锟斤拷一锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷 TNS_ADMIN 锟皆憋拷通锟斤拷 $TNS_ADMIN/tnsnames.ora 锟斤拷锟斤拷取锟斤拷锟斤拷确锟斤拷 web 锟截伙拷锟斤拷锟教可讹拷取锟斤拷锟侥硷拷锟斤拷

character_set

使锟斤拷 Oracle 锟酵伙拷锟剿匡拷锟斤拷确锟斤拷锟街凤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷要锟斤拷锟斤拷锟捷匡拷锟斤拷址锟斤拷锟斤拷锟狡ワ拷洹o拷锟斤拷锟斤拷匹锟戒,Oracle 锟结尽锟斤拷锟杰地斤拷锟斤拷锟捷达拷锟斤拷锟捷匡拷锟街凤拷锟斤拷锟斤拷锟斤拷转锟斤拷锟斤拷锟斤拷为锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟杰诧拷锟杰革拷锟斤拷锟斤拷锟矫的斤拷锟斤拷锟阶拷锟揭诧拷锟斤拷锟揭恍┦憋拷淇拷锟斤拷锟�

锟斤拷锟斤拷锟街革拷锟斤拷锟絆racle 锟酵伙拷锟斤拷锟斤拷 NLS_LANG 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷

锟斤拷锟捷此诧拷锟斤拷锟缴硷拷锟斤拷锟斤拷锟斤拷时锟戒。

session_mode

锟剿诧拷锟斤拷锟斤拷 PHP 5锟斤拷PECL OCI8 1.1锟斤拷锟芥本锟斤拷始锟斤拷锟矫o拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值锟斤拷OCI_DEFAULT锟斤拷OCI_SYSOPER 锟斤拷 OCI_SYSDBA锟斤拷锟斤拷为 OCI_SYSOPER 锟斤拷 OCI_SYSDBA 锟斤拷锟斤拷之一锟斤拷锟剿猴拷锟斤拷锟斤拷锟斤拷使锟斤拷锟解部锟斤拷证锟介建锟斤拷锟斤拷锟斤拷权锟斤拷锟斤拷锟接★拷锟斤拷锟斤拷权锟斤拷锟斤拷锟斤拷默锟斤拷锟角斤拷锟矫的★拷锟斤拷要锟斤拷 oci8.privileged_connect 锟斤拷为 On 锟斤拷锟斤拷锟矫★拷

PHP 5.3锟斤拷PECL OCI8 1.3.4锟斤拷锟斤拷锟斤拷锟斤拷 OCI_CRED_EXT 模式值锟斤拷使锟斤拷锟解部锟斤拷锟斤拷锟较低筹拷锟街わ拷锟斤拷锟斤拷锟� Oracle 锟斤拷锟捷匡拷锟叫斤拷锟斤拷锟斤拷锟矫★拷OCI_CRED_EXT 锟斤拷志只锟斤拷锟斤拷锟斤拷锟矫伙拷为 "/"锟斤拷锟斤拷锟斤拷为锟秸碉拷锟斤拷锟斤拷锟�oci8.privileged_connect 锟斤拷为 On 锟斤拷 Off锟斤拷

OCI_CRED_EXT 锟斤拷锟斤拷 OCI_SYSOPER 锟斤拷 OCI_SYSDBA 模式锟斤拷锟绞癸拷谩锟�

OCI_CRED_EXT 锟斤拷锟节帮拷全锟斤拷原锟斤拷支锟斤拷 Windows 系统锟斤拷

锟斤拷锟斤拷值

Returns a connection identifier or FALSE on error.

锟斤拷锟斤拷

Example #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";

?>

Example #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";

?>

Example #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";

?>

Example #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

?>

注锟斤拷

Note:

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

Note:

In PHP versions before 5.0.0 use ocilogon() instead. 锟节碉拷前锟芥本锟叫o拷锟缴的猴拷锟斤拷锟斤拷锟斤拷锟斤拷锟皆憋拷使锟矫o拷锟斤拷锟窖撅拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷使锟矫★拷

锟轿硷拷

  • oci_pconnect() - 使锟斤拷一锟斤拷锟街撅拷锟斤拷锟斤拷锟斤拷锟斤拷 Oracle 锟斤拷锟捷匡拷
  • oci_new_connect() - 锟斤拷锟斤拷一锟斤拷锟斤拷 Oracle 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
  • oci_close() - 锟截憋拷 Oracle 锟斤拷锟斤拷