PHP Manual
Prev |  | Next

# OCILogon

OCILogon -- Establishes a connection to Oracle

## Description

`int OCILogon`(string username, string password, string [db]);

OCILogon() returns an connection identifier needed for most other OCI calls. The optional third parameter can either contain the name of the local Oracle instance or the name of the entry in tnsnames.ora to which you want to connect. If the optional third parameter is not specified, PHP uses the environment variables ORACLE_SID (Oracle instance) or TWO_TASK (tnsnames.ora) to determine which database to connect to.

Connections are shared at the page level when using OCILogon(). This means that commits and rollbacks apply to all open transactions in the page, even if you have created multiple connections.

This example demonstrates how the connections are shared.

Example 1. OCILogon

      1
      2 <?php
      3 print "<HTML><PRE>";
      4 $db = "";
      5
      6 $c1 = ocilogon("scott","tiger",$db);
      7 $c2 = ocilogon("scott","tiger",$db);
      8
      9 function create_table($conn)
     10 { $stmt = ociparse($conn,"create table scott.hallo (test
     11 varchar2(64))");
     12   ociexecute($stmt);
     13   echo $conn." created table\n\n";
     14 }
     15
     16 function drop_table($conn)
     17 { $stmt = ociparse($conn,"drop table scott.hallo");
     18   ociexecute($stmt);
     19   echo $conn." dropped table\n\n";
     20 }
     21
     22 function insert_data($conn)
     23 { $stmt = ociparse($conn,"insert into scott.hallo values('$conn' || ' ' || to_char(sysdate,'DD-MON-YY HH24:MI:SS'))");
     24   ociexecute($stmt,OCI_DEFAULT);
     25   echo $conn." inserted hallo\n\n";
     26 }
     27
     28 function delete_data($conn)
     29 { $stmt = ociparse($conn,"delete from scott.hallo");
     30   ociexecute($stmt,OCI_DEFAULT);
     31   echo $conn." deleted hallo\n\n";
     32 }
     33
     34 function commit($conn)
     35 { ocicommit($conn);
     36   echo $conn." commited\n\n";
     37 }
     38
     39 function rollback($conn)
     40 { ocirollback($conn);
     41   echo $conn." rollback\n\n";
     42 }
     43
     44 function select_data($conn)
     45 { $stmt = ociparse($conn,"select * from scott.hallo");
     46   ociexecute($stmt,OCI_DEFAULT);
     47   echo $conn."----selecting\n\n";
     48   while (ocifetch($stmt))
     49     echo $conn." <".ociresult($stmt,"TEST").">\n\n";
     50   echo $conn."----done\n\n";
     51 }
     52
     53 create_table($c1);
     54 insert_data($c1);   // Insert a row using c1
     55 insert_data($c2);   // Insert a row using c2
     56
     57 select_data($c1);   // Results of both inserts are returned
     58 select_data($c2);
     59
     60 rollback($c1);      // Rollback using c1
     61
     62 select_data($c1);   // Both inserts have been rolled back
     63 select_data($c2);
     64
     65 insert_data($c2);   // Insert a row using c2
     66 commit($c2);        // commit using c2
     67
     68 select_data($c1);   // result of c2 insert is returned
     69
     70 delete_data($c1);   // delete all rows in table using c1
     71 select_data($c1);   // no rows returned
     72 select_data($c2);   // no rows returned
     73 commit($c1);        // commit using c1
     74
     75 select_data($c1);   // no rows returned
     76 select_data($c2);   // no rows returned
     77
     78
     79 drop_table($c1);
     80 print "</PRE></HTML>";
     81 ?>

See also OCIPLogon() and OCINLogon().

Prev | Home | Next
OCIBindByName | Up | OCIPLogon