Connecting to Oracle Using OCI8 : Oracle : Databases PHP Source Code


PHP Source Code » Databases » Oracle »

 

Connecting to Oracle Using OCI8


Oracle Connection Types
There are three ways to connect to an Oracle database in a PHP application, using
standard connections, unique connections, or persistent connections. Each method returns
a connection “resource” that is used in subsequent OCI8 calls.
Standard Connections
For basic connection to Oracle use PHP’s oci_connect() call:
$c = oci_connect($username, $password, $dbname);
You can call oci_connect() more than once in a script. If you do this and use the same
username and database name, then you get a pointer to the original connection.
Multiple Unique Connections
To get a totally independent connection use oci_new_connect():
$c = oci_new_connect($username, $password, $dbname);
Each connection is separate from any other. This lets you have more than one database
session open at the same time. This can be useful when you want to do database operations
independently from each other.
Persistent Connections
Persistent connections can be made with oci_pconnect():
$c = oci_pconnect($username, $password, $dbname);
Persistent connections are not automatically closed at the end of a PHP script and remain
open in PHP’s persistent connection cache for reuse in other scripts.This makes
oci_pconnect() fast for frequently used applications.

Full Database Connection String
The full Oracle Net connection string gives total flexibility over the connection.

$db = '(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)
(HOST = mymachine.mydomain)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = MYDB.MYDOMAIN)))';
$c = oci_connect($username, $password, $db);

<?php
function do_query($c$query)
{
$s oci_parse($c$query);
oci_execute($s);
oci_fetch_all($s$res);
echo 
"<pre>";
var_dump($res);
echo 
"</pre>";
}
$c1 oci_connect('hr''hrpwd''//localhost/XE');
$c2 oci_connect('hr''hrpwd''//localhost/XE');
do_query($c1'select user from dual');
oci_close($c1);
do_query($c1'select user from dual'); // fails
do_query($c2'select user from dual'); // succeeds
oci_close($c2);
?>


HTML code for linking to this page:

Follow Navioo On Twitter

PHP Source Code

 Navioo Databases
» Oracle