Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Oracle Functions : oci_connect

oci_connect

Establishes a connection to the Oracle server (PHP 5, PECL oci8:1.1-1.2.4)
resource oci_connect ( string username, string password [, string db [, string charset [, int session_mode]]] )

Example 1637. 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($stmt, OCI_DEFAULT);
 echo
$conn . " inserted hallo\n\n";
}

function
delete_data($conn)
{
 
$stmt = oci_parse($conn, "delete from scott.hallo");
 
oci_execute($stmt, OCI_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($stmt, OCI_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>";
?>
Examples ( Source code ) » oci_connect

<?
$c 
oci_connect("hr""hrpwd""//localhost/XE");
$s oci_parse($c"select region_name,
           regions.region_id as myreg,
           country_name,
           countries.region_id
            from countries
            inner join regions
            on countries.region_id = regions.region_id"
);
     
oci_execute($s);
while (
$res oci_fetch_array($s)) {
      echo 
$res["REGION_NAME"] . " " $res["MYREG"] . " - " .
      
$res["COUNTRY_NAME"] . " " $res["REGION_ID"] . " " .
      
"<br>\n";
}
?>
The query column alias MYREG is used as the index to the result array. The script output is:
Americas 2 - Argentina 2
Asia 3 - Australia 3
Europe 1 - Belgium 1
Americas 2 - Brazil 2
Americas 2 - Canada 2

Related Examples ( Source code ) » oci_connect
















Code Examples / Notes » oci_connect

sebastien.barbieri _at_ gmail dot com

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.


a01b02_no_spam

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


domenico a01b20_nospam_

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


chris

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.

andrei

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))
)
');
?>


greatval gmail com

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. :-)


Change Language


Follow Navioo On Twitter
oci_bind_array_by_name
oci_bind_by_name
oci_cancel
oci_close
OCI-Collection->append
OCI-Collection->assign
OCI-Collection->assignElem
OCI-Collection->free
OCI-Collection->getElem
OCI-Collection->max
OCI-Collection->size
OCI-Collection->trim
oci_commit
oci_connect
oci_define_by_name
oci_error
oci_execute
oci_fetch_all
oci_fetch_array
oci_fetch_assoc
oci_fetch_object
oci_fetch_row
oci_fetch
oci_field_is_null
oci_field_name
oci_field_precision
oci_field_scale
oci_field_size
oci_field_type_raw
oci_field_type
oci_free_statement
oci_internal_debug
OCI-Lob->append
OCI-Lob->close
oci_lob_copy
OCI-Lob->eof
OCI-Lob->erase
OCI-Lob->export
OCI-Lob->flush
OCI-Lob->free
OCI-Lob->getBuffering
OCI-Lob->import
oci_lob_is_equal
OCI-Lob->load
OCI-Lob->read
OCI-Lob->rewind
OCI-Lob->save
OCI-Lob->saveFile
OCI-Lob->seek
OCI-Lob->setBuffering
OCI-Lob->size
OCI-Lob->tell
OCI-Lob->truncate
OCI-Lob->write
OCI-Lob->writeTemporary
OCI-Lob->writeToFile
oci_new_collection
oci_new_connect
oci_new_cursor
oci_new_descriptor
oci_num_fields
oci_num_rows
oci_parse
oci_password_change
oci_pconnect
oci_result
oci_rollback
oci_server_version
oci_set_prefetch
oci_statement_type
ocibindbyname
ocicancel
ocicloselob
ocicollappend
ocicollassign
ocicollassignelem
ocicollgetelem
ocicollmax
ocicollsize
ocicolltrim
ocicolumnisnull
ocicolumnname
ocicolumnprecision
ocicolumnscale
ocicolumnsize
ocicolumntype
ocicolumntyperaw
ocicommit
ocidefinebyname
ocierror
ociexecute
ocifetch
ocifetchinto
ocifetchstatement
ocifreecollection
ocifreecursor
ocifreedesc
ocifreestatement
ociinternaldebug
ociloadlob
ocilogoff
ocilogon
ocinewcollection
ocinewcursor
ocinewdescriptor
ocinlogon
ocinumcols
ociparse
ociplogon
ociresult
ocirollback
ocirowcount
ocisavelob
ocisavelobfile
ociserverversion
ocisetprefetch
ocistatementtype
ociwritelobtofile
ociwritetemporarylob
eXTReMe Tracker