Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

Oracle

Posted Nov 25, 2002

Abstracting Oracle Connectivity with PHP/OCI8 - Page 2

By DatabaseJournal.com Staff



Wrapping it up into a PHP Class

I've briefly pointed to a couple functions that we might want to build that will abstract some of the common Oracle 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);
    }

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

//######################################################################
//## END OF CLASS
//######################################################################


Page 3: How Do I Use This?




Oracle Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 




Latest Forum Threads
Oracle Forum
Topic By Replies Updated
Oracle Data Mining: Classification jan.hasller 0 July 5th, 07:19 AM
Find duplicates - Unique IDs Lava 5 July 2nd, 08:30 AM
no matching unique or primary key rcanter 1 April 25th, 12:32 PM
Update values of one table based on condition of values in other table using Trigger Gladiator 3 February 29th, 06:01 PM


















Thanks for your registration, follow us on our social networks to keep up-to-date