Abstracting Oracle Connectivity with PHP/OCI8 - Page 2

November 25, 2002



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?









The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers