Home arrow PHP arrow Page 3 - Abstracting Oracle Connectivity with PHP/OCI8
PHP

Abstracting Oracle Connectivity with PHP/OCI8


In this article Lorenzo explains a neat utility to simplify connecting to an Oracle database using PHP/OCI8. This is sure to make DB operations between PHP and Oracle much easier and faster.

Author Info:
By: Dante Lorenso
Rating: 4 stars4 stars4 stars4 stars4 stars / 19
January 08, 2003
TABLE OF CONTENTS:
  1. · Abstracting Oracle Connectivity with PHP/OCI8
  2. · Oracle How-To The Hard Way
  3. · Wrapping it up into a PHP Class
  4. · How Do I Use This?
  5. · Those Pretty OCIBindByName Arguments
  6. · Switching Between Development, QA and Production Environments
  7. · Conclusion

print this article
SEARCH DEVARTICLES

Abstracting Oracle Connectivity with PHP/OCI8 - Wrapping it up into a PHP Class
(Page 3 of 7 )

I've briefly pointed to a couple functions that we might want to build that'll abstract some of the Oracle common connectivity issues. But the real magic starts to happen once we take these functions and roll them into a tight little ball of OCI8 and functioning code.

The idea here is that we'll create a class called 'OCIHook' that will act as our one-and-only method of talking to Oracle. If you do this, we are guaranteed that every oracle call is done the same way and that all the logins and passwords will come out of our vault.

Hopefully by using this simple API on top of the PHP built-in functionality, we'll save coding time and energy.

So, let's get to it. here is the OCI8Hook class:

//#########################################
//## Written by D. Dante Lorenso
//## Free. Do what you want with this.
//## Send money if you can.
//#########################################
class OCI8Hook
{
var $ERROR = "";

//-----------------------------------------
/** PRIVATE
* Returns the SID, USERNAME, and PASSOWORD used to connect to a given
* Oracle database.
*/
function getDBAuth($sid)
{

switch (strtoupper($sid))
{
//case "DBXYZ": return (array("usernam1", "secret1", "DBXYZ"));
case "DBXYZ": return (array("usernam1", "secret1", "TESTDB"));
case "DBABC": return (array("usernam2", "secret2", "DBABC"));
case "DB123": return (array("usernam3", "secret3", "DB123"));
case "DBHJK": return (array("usernam4", "secret4", "DBHJK"));
}

// I don't know what to do with this host/SID.
return(false);
}

//-----------------------------------------
/** PRIVATE
* Logs the current message in OCIError to the Apache Log file. This is
* done by first including an application-level error code, and the
* current PHP page identifier.
*/
function dumpError($errcode, $errhndl=0) {
// retrieve the error message...
$error = ($errhndl) ? OCIError($errhndl) : OCIError();

// clean the message...
$this->ERROR = trim($error["message"]);

// log this error to Apache's error log
error_log(sprintf("%s %s %s %s",
$errcode, $_SERVER["PHP_SELF"], $error["code"], $this->ERROR));

}

//-----------------------------------------
/** PUBLIC (stmt_hndl)
* Returns the statement handle created from the execution of the SQL
* query. If the statement fails, the method returns (false) and the
* error is available in $this->ERROR.
*/
function query($sid, $sql, &$bargs) {
// clear any previous errors.
$this->ERROR = "";

// look up the username, password, and database for this SID
$dbauth = $this->getDBAuth($sid);

if (empty($dbauth) || ! is_array($dbauth))
{
$this->ERROR = "Database Error(1).";
return(false);
}

// connect to the database...
$dbh = @OCILogon($dbauth[0], $dbauth[1], $dbauth[2]);

if (!$dbh) {
$this->dumpError("OCILogon");
return (false);
}

// parse the SQL statement...
$stmt = @OCIParse($dbh, $sql);
if (!$stmt)
{
$this->dumpError("OCIParse", $stmt);
return (false);
}

// Bind the args into the statement... (ARG[0], VALUE[1], LEN[2])
foreach ($bargs as $barg) {
$$barg[0] = $barg[1];
@OCIBindByName($stmt, ":$barg[0]", $$barg[0], $barg[2]);

}

// execute sql query
$rslt = @OCIExecute($stmt);
if (!$rslt) {
$this->dumpError("OCIExecute(STMT)", $stmt);
$this->dumpError("OCIExecute(RSLT)", $rslt);
return (false);
}

// if there are bind args, recover them...
$r_bargs = array();
foreach ($bargs as $barg) {
$r_bargs[$barg[0]] = $$barg[0];
}
$bargs = $r_bargs;

// return the sql statement handle upon success
return ($stmt);
}

//-----------------------------------------
}

blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials