 |
|
ss
Oracle Tips by Burleson |
Connecting to Oracle
There are two PHP functions used to connect to an
Oracle instance:
oci_connect ( string username,
string password,
string db,
string charset);
and:
oci_new_connect( string
username,
string password,
string db,
string charset);
EXAMPLE:
$db=oci_new_connect(“SCOTT”, “TIGER”,”MYDB”);
The parameters “db” and “charset” are not
mandatory. The “db” parameter, also known as “database descriptor” or
“TNS descriptor”, can be omitted if the oracle_sid variable is
defined in the environment, or if the variables defining the default
database location are defined in the environment.
On Unix/Linux, the default database is defined
through the variable two_task; while on Windows, the variable
LOCAL must be defined in the registry, under the “ALL_HOMES” registry
directory. The syntax of both variables is the same:
export TWO_TASK=”local”
Or the value of the LOCAL registry key can be set
to the value of the default database as shown in the figure below.
This is done by the normal registry editor
available on any Windows system. If the database version is 9.2 or
higher, the character set can be specified. Otherwise, the character
set argument is quietly ignored. Both functions return the database
handle, as shown in Examples 13 and 13a. So, there are two functions
with the same syntax, same return value and different names. What then
is the difference?
The difference is the oci_connect()
function is used to create a so called “permanent connection”, while
the function, oci_new_connect(), creates a new
connection each time it is used. HTTP is a stateless protocol and does
not create permanent connections. What are “permanent database
connections”?
Normally, when using the “new connect” function,
the connection to the Oracle database is dropped by the httpd process
or thread. In the case of Windows, it is dropped when the script
execution is finished.
To that end, the destructor is included in the
OCI_Session class. In case of so-called permanent connections, PHP
checks whether the httpd process executing the script is already
connected to the database and whether the username and password are
the same. If they are the same, the oci_connect() function
returns the existing handle.
Only if the parameters do not match is the new
database handle created. Unfortunately, early versions of PHP5 and
OCI8 module were extremely buggy and didn’t work properly. The
situation was partially fixed in 5.1.0 and later versions, but one
problem still remains: when database is restarted, Apache is no longer
able to establish a connection to Oracle using PHP5 module.
Problems
with the persistent connections were the reason for using the
oci_new_connect()function
in the OCI_Session class. PHP5 and Apache2 are rather new. The error
above did not appear on Apache 1.3.29, it only appeared on Apache
2.0.52. These combinations are sensitive and can cause much grief and
pain. However, it is good to use the oci_new_connect() function
whenever possible. When the bugs with the permanent connections are
fixed, all that needs changing is the OCI_Session class. Blessed be
the OO (Object Oriented) programming!
See
code depot for complete scripts
The above book excerpt is from:
Easy Oracle
PHP
Create Dynamic Web Pages with Oracle Data
ISBN
0-9761573-0-6
Mladen Gogala
http://www.rampant-books.com/book_2005_2_php_oracle.htm
|