 |
|
Connecting to Oracle
Oracle Tips by Burleson Consulting |
ADOdb follows a similar pattern of DSN (Data
Source Name) because it was modeled after the same original as PEAR.
The original are, of course URL-like database names used by JDBC
drivers. The DSN syntax for Oracle is:
$DSN=driver://username:password@database
The “database” part in the expression above is
the TNS descriptor of the database to which the connection is being
established. The driver is “oci8”, same as in the case of PEAR DB.
When the DSN is defined, the new connection is
established by the following sequence:
require_once('adodb/adodb.inc.php');
$DSN=”oci8://scott:tiger@local”;
$db=NewADOConnection($DSN);
The reader should notice the inclusion of the
file, adodb.inc.php. This file defines the basic ADOdb
classes and must be included if ADOdb is to be used.
The code on the previous page creates a connection
to the SCOTT schema on the database called “LOCAL”. As with PEAR DB,
DSN can be split into separate fields for the database type, username,
password and database descriptor.
require_once('adodb/adodb.inc.php');
$db=NewADOConnection('oci8');
$db->Connect($database,”scott”,”tiger”);
The variable $database represents
the TNS descriptor for the desired Oracle database. For non-Oracle
databases, the syntax is different and should be checked in the
on-line manual.
In the PEAR DB section, connecting to an Oracle
instance was explained in Example 20. The following example is Example
20 rewritten using ADOdb:
#!/usr/local/bin/php
<?php
require_once('adodb/adodb.inc.php');
require_once('adodb/adodb-exceptions.inc.php');
try {
$db=NewADOConnection("oci8");
@$db->Connect("local","scott","lion");
$sql="select * from emp";
$sth=$db->Execute($sql);}
catch (Exception $e) {
echo "Error code is:".$e->getCode() ."\n";
die($e->getMessage());
}
$ncols=$sth->FieldCount();
echo "This result has ".$ncols." columns\n";
foreach (range(0,$ncols-1) as $ind) {
$col=$sth->FetchField($ind);
echo $col->name."\t";
}
echo "\n";
while ($row=$sth->FetchRow())
{
foreach (range(0,$ncols-1) as $ind) {
echo $row[$ind]."\t";
} echo "\n";
}
?>
Notice the password for the user SCOTT was
specified as “lion”, obviously incorrect. When the example is
executed, the following results:
$
./example25.php
Error code is:1017
oci8 error: [1017: ORA-01017: invalid username/password; logon
denied
] in EXECUTE("select * from emp")
$
ADOdb works with PHP5 and throws an exception
when an error occurs. To initiate such behavior, the inclusion of
the adodb-exceptions.inc.php file is needed, in addition to the
adodb.inc.php file, as was done in the header. ADOdb also differs
from PEAR DB philosophically, because ADOdb exceptions return the
error code belonging to the underlying database, while PEAR DB
returns its own error, portable across all supported databases.
Also notice the error was returned in the EXECUTE,
not in the connect function. ADOdb was built for speed and attempts to
pack as many operations together as possible. By actually trying to
connect only before the execute, ADOdb can potentially save a trip
across the network. This can lead to errors being returned in
unexpected places. That is exactly what makes exceptions such a good
tool for exception handling.
The problematic password is replaced with the
correct password and the following results:
The result is exactly the same as in Example 20.
Methods such as FieldCount()and FetchField() are explained in the next section.
They do pretty much what they are expected to do. The only important
function in Example 25 that was not explained before is the range
function. The range function is a very useful function that creates a
range of numbers in a form of an array and is very useful in the
foreach expressions. The following shows what the online manual says
about the range function:
range -- Create an array containing a range of
elements
Description
range() returns an array of elements from
low to high,
inclusive. If low > high, the sequence will be from high to low.
New Parameter
The optional step
parameter was added in 5.0.0. If a step
value is given, it will be used as the increment between elements in
the sequence. step should be given as a
positive number. If not specified, step
will default to 1.
SEE CODE DEPOT FOR FULL SCRIPTS
http://www.rampant-books.com/book_2005_2_php_oracle.htm
 |
Expert Remote DBA
BC is America's oldest and largest Remote DBA Oracle support
provider. Get real Remote DBA experts, call
BC Remote DBA today. |
 |
|