Free Newsletters:
DatabaseJournal  
DBANews
Database Journal
Search Database Journal:
 
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum DBA Videos
internet.com

» Database Journal Home
» Database News
» DBA Videos
» Database Articles
» Database Tutorials
MS SQL
Oracle
MS Access
MySQL
DB2
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» DBA Jobs
» Sitemap

News Via RSS Feed



follow us on Twitter

Marketplace Partners
Be a Marketplace Partner

internet.commerce
Be a Commerce Partner


















Apple: 1M New iPhones Sold, Apologies for Snafus

T-Mobile's Next Android Phone: myTouch 3G

Firms Push Cloud, Virtualization for IT Niches

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Database Journal | DBA Support | SQLCourse | SQLCourse2







Data Entry / Customer Service with Insurance Experience
The Computer Merchant, Ltd
US-FL-Sarasota

Justtechjobs.com Post A Job | Post A Resume

Featured Database Articles

Oracle

November 25, 2002

Abstracting Oracle Connectivity with PHP/OCI8



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?


Go to page: Prev  1  2  3  4  5  Next  

Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

Oracle Archives








Latest Forum Threads
Oracle Forum
Topic By Replies Updated
Problem with archive_log_format parameter in SPFILE dave_keyur 6 June 2nd, 02:14 AM
getting first date and last date of previous month in oracle Osho4U 8 May 26th, 05:29 PM
Removing middle initials antgaudi 1 May 12th, 10:40 AM
Extracting the smallest age. MIN with SYSDATE? antgaudi 2 May 11th, 01:45 AM