12.4 The Oracle Module


The past two modules have been kind of straightforward to implement, so why not throw in a hard one? Oracle is an industrial-strength database used on all the major Web sites, including Yahoo and Amazon.com. More information about Oracle can be found at http://www.oracle.com/.

 DB/oracle.php <?php include_once("DB/standard.php"); function db_connect($args=array()) {     array_shift($args);     $conn = @ora_logon($args[0], $args[1]);     $dbh = @ora_open($conn);     return($dbh); } function db_pconnect($args=array()) {     array_shift($args);     $conn = @ora_plogon($args[0], $args[1]);     $dbh = @ora_open($conn);     return($dbh); } function db_db_query($args=array()) {     return db_query($args); } function db_query($args=array()) {     if (@ora_parse($args[0], $args[1]) < 0) {         return(false);     }     @ora_exec($args[0]);     return($args[0]); } function db_fetchrow($args=array()) {     switch ($args[1]) {         case DB_GETMODE_ASSOC:             $cols = @ora_fetch_into($args[0], &$rows, ORA_FETCHINTO_ASSOC);             break;         case DB_GETMODE_NULL:             $cols = @ora_fetch_into($args[0], &$rows, ORA_FETCHINTO_NULLS);             break;         default:              $cols = @ora_fetch_into($args[0], &$rows);              break;     }     if ($cols) {         return($rows);     }     return(false); } function db_prepare($args=array()) {     return db_simulate_prepare($args); } function db_execute($args=array()) {     $stmt = db_simulate_execute(&$args);     return db_fetchrow(array($stmt, array_shift ($args))); } function db_num_rows($args=array()) {     return @ora_numrows($args[0]); } function db_commit($args=array()) {     return @ora_commit($args[0]); } function db_rollback($args=array()) {     return @ora_rollback($args[0]); } function db_autoCommit($args=array()) {     return $args[1] ? @ora_commiton($args[0]) : @ora_commitoff($args[0]); } function db_free_result($args=array()) {     return @ora_close($args); } function db_close($args=array()) {     return @ora_logoff($args[0]); } ?> 

Explanation

In the preceding extension we use PHP's Oracle extension, not the OCI8 extension, to create our module. We do this because the Oracle extension, although not as powerful as the OCI8 extension, is more akin to the rest of the PHP extensions.

The structure of an Oracle call sequence goes something like this:

 Connect  Open  Query  Fetch  Close  Disconnect 

This is different from most other databases, which have the following sequence of operations:

 Connect  Query  Fetch  Disconnect 

Therefore, we must combine the Connect and Open stages as well as the Close and Disconnect stages in order for the Oracle module to work.

When creating a database-independent API, the feature set is limited because PHP and other databases don't support some of the features that Oracle supports. If you need the advanced features of Oracle, I suggest that you check out the documentation for the Oracle and OCI8 modules.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net