PHP
  Home arrow PHP arrow Page 3 - Abstracting Oracle Connectivity with PHP/O...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Abstracting Oracle Connectivity with PHP/OCI8
By: Dante Lorenso
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 14
    2003-01-08

    Table of Contents:
  • Abstracting Oracle Connectivity with PHP/OCI8
  • Oracle How-To — The Hard Way
  • Wrapping it up into a PHP Class
  • How Do I Use This?
  • Those Pretty OCIBindByName Arguments
  • Switching Between Development, QA and Production Environments
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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);
    }

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

    More PHP Articles
    More By Dante Lorenso


     

    PHP ARTICLES

    - 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
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT