PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

oci_define_by_name> <oci_commit
Last updated: Fri, 18 Jul 2008

view this page in

oci_connect

(PHP 5, PECL oci8:1.1-1.2.4)

oci_connect — Establishes a connection to the Oracle server

Descrizione

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

Returns a connection identifier needed for most other OCI calls.

Elenco dei parametri

username

The Oracle user name.

password

The password for username .

db

This optional parameter can either contain the name of the local Oracle instance or the name of the entry in tnsnames.ora.

If the not specified, PHP uses environment variables ORACLE_SID and TWO_TASK to determine the name of local Oracle instance and location of tnsnames.ora accordingly.

charset

Utilizzando Oracle server version 9.2 o successive, si può specificare il parametro charset , che verrà utilizzato nella nuova connessione. Se si utilizza Oracle server < 9.2, questo parametro verrà ignorato ed al suo posto verrà invece utilizzata la variabile d'ambiente NLS_LANG.

session_mode

This parameter is available since version 1.1 and accepts the following values: OCI_DEFAULT, OCI_SYSOPER and OCI_SYSDBA. If either OCI_SYSOPER or OCI_SYSDBA were specified, this function will try to establish privileged connection using external credentials. Privileged connections are disabled by default. To enable them you need to set oci8.privileged_connect to On.

Valori restituiti

Returns a connection identifier or FALSE on error.

Esempi

Example #1 oci_connect() example

<?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>";
?>

Note

Nota: If you're using PHP with Oracle Instant Client, you can use easy connect naming method described here: » http://download-west.oracle.com/docs/cd/B12037_01/network.101/b10775/naming.htm#i498306. Basically this means you can specify "//db_host[:port]/database_name" as database name. But if you want to use the old way of naming you must set either ORACLE_HOME or TNS_ADMIN.

Nota: The second and subsequent calls to oci_connect() with the same parameters will return the connection handle returned from the first call. This means that queries issued against one handle are also applied to the other handles, because they are the same handle. This behaviour is demonstrated in Example 1 below. If you require two handles to be transactionally isolated from each other, you should use oci_new_connect() instead.

Nota: In PHP versions before 5.0.0 you must use ocilogon() instead. This name still can be used, it was left as the alias of oci_connect() for downwards compatability. This, however, is deprecated and not recommended.



oci_define_by_name> <oci_commit
Last updated: Fri, 18 Jul 2008
 
add a note add a note User Contributed Notes
oci_connect
sixd at php dot net
21-Jul-2008 01:16
From PHP 5.3 onwards:

A new OCI_CRED_EXT flag can be passed as the "session_mode" parameter
to oci_connect(), oci_new_connect() and oci_pconnect().

  $c1 = oci_connect("/", "", $db, null, OCI_CRED_EXT);

This tells Oracle to do external or OS authentication, if configured
in the database.

OCI_CRED_EXT can only be used with username of "/" and a empty
password.  Oci8.privileged_connection may be On or Off. 

OCI_CRED_EXT is not supported on Windows for security reasons.

The new flag may be combined with the existing OCI_SYSOPER or
OCI_SYSDBA modes (note: oci8.privileged_connection needs to be On for
OCI_SYSDBA and OCI_SYSOPER), e.g.:

  $c1 = oci_connect("/", "", $db, null, OCI_CRED_EXT+OCI_SYSOPER);
sixd at php dot net
30-Jun-2008 04:33
If you want to specify a connection timeout in case there is network problem, you can edit the client side (e.g. PHP side) sqlnet.ora file and set SQLNET.OUTBOUND_CONNECT_TIMEOUT. This sets the upper time limit for establishing a connection right through to the DB, including the time for attempts to connect to other services.   It is available from Oracle 10.2.0.3 onwards.

In Oracle 11.1, a slightly lighter-weight solution TCP.CONNECT_TIMEOUT was introduced.  It also is a sqlnet.ora parameter.  It bounds just the TCP connection establishment time, which is mostly where connection problem are seen.

The client sqlnet.ora file should be put in the same directory as the tnsnames.ora file.
sxid at php dot net
20-Jun-2008 03:46
If PHP is built with Oracle 9.2 or greater client libraries, the
charset parameter is used to determine the character set used by the
Oracle client libraries.  When PHP is built with older Oracle client
libraries, or if the parameter is not passed, Oracle NLS environment
variables such as NLS_LANG will be used to determine the character
set.

The character set does not need to match that used by the database.
If it doesn't, Oracle will do its best to convert data to and from the
database character set.  Depending on the character sets, this may not
which may always be give usable or valid results, and it can reduce
overall performance

Because passing the parameter removes the environment look up, it can
improve connection performance
sebastien.barbieri _at_ gmail dot com
13-Sep-2006 09:42
When you are using Oracle 9.2+ I would say that you MUST use the CHARSET parameter.

Of course, you will not notice it until there is accented character... so just specify it and you will avoid a big headache.

So for example here is our Oracle internal conf:
select * from nls_database_parameters;
 
PARAMETER                      VALUE
------------------------------ ----------------------------------------

NLS_LANGUAGE                   AMERICAN
NLS_TERRITORY                  AMERICA
NLS_ISO_CURRENCY               AMERICA
NLS_CHARACTERSET               WE8ISO8859P15

 
And there our oci_connect call:

$dbch=ocilogon($user,$pass,$connectString,"WE8ISO8859P15");

Without that, you will get question mark (inversed), squares… instead of most accented character.

Don’t forget to use that for writing as well as for reading.
greatval <wow> gmail <dot> com
24-Jul-2006 09:30
For use PHPv5 functions in PHPv4 i use simple script:
<?php
$funcs
=array(
       
'oci_connect'=>'OCILogon',
       
'oci_parse'=>'OCIParse',
       
'oci_execute'=>'OCIExecute',
       
'oci_fetch'=>'OCIFetch',
       
'oci_num_fields'=>'OCINumCols',
       
'oci_field_name'=>'OCIColumnName',
       
'oci_result'=>'OCIResult',
       
'oci_free_statement'=>'OCIFreeStatement',
);
// yoy can add yours pairs of funcs.

foreach ($funcs as $k=>$v)
    {
        if (!
function_exists($k))
            {
               
$arg_string='$p0';
                for (
$i=1;$i<20;$i++) {
                   
$arg_string.=',$p'.$i;
                }
                eval (
'function '.$k.' () {
                        list('
.$arg_string.')=func_get_args();
                        return '
.$v.'('.$arg_string.');
                        }
                '
);
            }
    }
?>

simple, but it work. :-)
Andrei
07-Nov-2005 05:08
lost oracle connection. need restart apache?

Temporarely you can prevent 'connection lost' by using folowing script (use it at your own risk):

<?php
$rnum
=rand(0,99999999);
$dbcon = oci_new_connect('XXXXX', 'XXXXXX',
'
(DESCRIPTION =
           (ADDRESS =
        (PROTOCOL = TCP)
        (HOST = XXX.XXX.XXX.XXX)
        (PORT = 1521)
        (HASH = '
.$rnum.')
     )
         (CONNECT_DATA =(SID = XXX))
     )
'
);
?>
Domenico a01b20_NOSPAM_ at iol dot it
07-Nov-2005 12:44
This note is an addendum to note#58378
Seems to be a good workaround set the oracle_home and/instead of the tns_admin.
tnsnames.ora must to be located in
$ORACLE_HOME/network/admin
and in
$TNS_ADMIN/ (if you use it)

---
Best Regards,
Domenico
a01b02_NO_SPAM at iol dot it
02-Nov-2005 02:44
Using tnsnames.ora
Apache 2
php 5.0.5
Oracle 10 IstantClient

PHP half of times return this error:

OCISessionBegin: ORA-24327: need explicit attach before authenticating a user in ...

In Oracle manual I find:

ORA-24327 need explicit attach before authenticating a user

    Cause: A server context must be initialized before creating a session.
    Action: Create and initialize a server handle.

I resolved using Easy Connect Naming Method.

Notice of this problem in bug#29779.

---
Best Regards,
Domenico
Chris
27-Oct-2005 05:19
Our tnsnames.ora uses the SERVICE_NAME=mydb - which for some reason wont work with PHP even though it works fine with tnsping. Using SID=mydb worked and a connection was established.

oci_define_by_name> <oci_commit
Last updated: Fri, 18 Jul 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites