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


















Wi-Fi Breaches Found in iPhone, Android Devices

Microsoft Shows Off Silverlight 4, IE9 Plans

SAP, Microsoft Gang Up on Oracle

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







Technical Lead
Thomson Reuters (Markets) LLC
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Featured Database Articles

Oracle

November 25, 2002

Abstracting Oracle Connectivity with PHP/OCI8

Dante Lorenso

The Problem

So, you have a huge project that you want to build. It's been determined that you want to use PHP, and your backend is Oracle. You've installed Oracle, you've installed your web server, and you've even gotten PHP to connect to your Oracle back end to do some simple queries. Now what?

If you are like most lazy programmers, you get something to work, then you look for a way to do the same thing without having to cut and paste code or retype the same thing over and over. Well, I've found that connecting to Oracle is typically done the same way over and over. So, I've written some functions to help me run some queries without the drudgery of repetition. Take these functions, roll them into a PHP class, and voila! ... easy PHP/OCI8!


Oracle How-To - The Hard Way

To do any database calls, the typical process is as follows:

  1. Connect to the database (OCILogon)
  2. Parse a SQL statement (OCIParse)
  3. Bind Variables if necessary (OCIBindByName)
  4. Execute query (OCIExecute)
  5. Fetch Results

Meanwhile, during each step in this process you need to check for errors and handle them appropriately. You might encounter an error during the OCILogon or during the OCIParse or during...well, you get the point.


Abstract the SQL Query Calls

What I've attempted to do is abstract this process into a single function prototyped as follows:


    /** 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) {
    ...
    }

You may notice that this function takes three arguments: SID, SQL, and Bind Args.

SID - The SID is the Oracle database you want to connect to. You'll notice that I'm not sending the username and password. You'll see why when you read the next section on abstracting the OCILogin.

SQL - The SQL parameter is the SQL statement you want to run. This is a SELECT, INSERT, UPDATE command...whatever. You might even be calling a chunk of PL/SQL code. Regardless of the query, this function can handle it.

Bind Args - The third parameter is an array of Bind arguments. Bind arguments allow you to pass IN/OUT variables to your SQL queries and enable a syntax of doing queries that don't require you to escape all your inputs. Once I discovered the use of bind arguments, my Oracle world was forever changed.


Abstract the OCILogin (Database, Username, and Password)

Another common problem in the Oracle connectivity world is where the passwords are stored. I've seen websites where Oracle usernames and passwords were sprinkled all over the place in this script and in that class. This makes it very difficult to maintain passwords or let alone go through the nightmare of trying to change one! What I've done is create another function to act as a password vault. I store the Oracle SID, username, and password as an array of three values which can be fetched by a given key. This way any time I want to connect to the "XYZ" database, I simply ask this function for the Account database name, username and password and get them. Here is a prototype for that function:


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


Page 2: Wrapping it up into a PHP Class


Go to page: 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
PL/SQL procedure help alexmb1 1 November 20th, 04:16 PM
Oracle Failsafe Errors : fs-11081 fs-10751 fs-10755, Running 3.4.1.6 on Win Server 20 lthai 0 November 20th, 02:47 PM
T-SQL functions in ORACLE aklein2003 0 November 18th, 07:28 AM
set difference query alexmb1 2 November 13th, 02:27 PM








internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

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