database,abstraction,layer,code,written,with,
Quick Search for:  in language:    
database,abstraction,layer,code,written,with,
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
PHP Stats

 Code: 74,754 lines
 Jobs: 12 postings

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for PHP.
Smart Image Navigator
By Brian Di Croce on 10/27


Click here to see a screenshot of this code!Form security verification
By Christian Mallette on 10/26

(Screen Shot)

Click here to see a screenshot of this code!Guestbook
By Nightmare on 10/25

(Screen Shot)

Shout! Box
By Nightmare on 10/23


SERVER-TO-SERVE R site (or single dir) transfer
By Raffaele Marranzini on 10/22


Site IP Logger - Watch who comes to your site!
By KRaZY-DeSiGN on 10/21


IP Banner - Block unwanted people from your site!
By KRaZY-DeSiGN on 10/20


PWGen
By Flinn Mueller on 10/19


URHere
By Flinn Mueller on 10/19


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

Database independence object wrapper

Print
Email
 
VB icon
Submitted on: 10/29/2000 5:49:44 PM
By: PHP Code Exchange  
Level: Advanced
User Rating: Unrated
Compatibility:PHP 3.0, PHP 4.0

Users have accessed this code 3415 times.
 
 
     This is a database abstraction layer, so that code can be written to run with any database. If you wish to switch database types, just change a parameter in the abstraction class ... not your entire code!! Currently supports mysql and postgres, I hope to add oracle and others. If you have any comments/suggestions, pls email me at manik@post1.com by Manik Surtani
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!

    //**************************************
    //     
    // Name: Database independence object wr
    //     apper
    // Description:This is a database abstra
    //     ction layer, so that code can be written
    //     to run with any database. If you wish to
    //     switch database types, just change a par
    //     ameter in the abstraction class ... not 
    //     your entire code!! Currently supports my
    //     sql and postgres, I hope to add oracle a
    //     nd others. If you have any comments/sugg
    //     estions, pls email me at manik@post1.com
    //     by Manik Surtani
    // By: PHP Code Exchange
    //**************************************
    //     
    
    <?php
    //--------------------------------------
    //     -----------------
    // DATABASE.INDEPENDENCE.OBJECT.WRAPPER
    // VERSION 0.30
    // VERSION DATE: 15th July 1999
    //--------------------------------------
    //     -----------------
    // Author: Manik Surtani <manik@post1
    //     .com>
    //--------------------------------------
    //     -----------------
    //--------------------------------------
    //     -----------------
    // This version currently supports POSTG
    //     RESQL and MYSQL
    // databases. More support to be added s
    //     ometime .... !!
    //--------------------------------------
    //     -----------------
    // This wrapper provides 'physical' data
    //     base connectivity
    // and works as the link between the php
    //     3-based web site 
    // and the selected database. 
    //--------------------------------------
    //     -----------------
    // set the database TYPE. 
    //$database_type==1 --> POSTGRES
    //$database_type==2 --> MYSQL
    $database_type=2;
    /*
    ---------------------------------------------------------
    "class resultSet" deals with a recordset produced by a
    databse query.
    ---------------------------------------------------------
    */
    class resultSet {
    var $element = array();
    var $column_name = array();
    var $numrows;
    var $numcols;
    function getColumn_by_name($rowNum,$colName) {
    $j=-1;
    for($i=0;$i<$this->numcols;$i++)
    {
    if ($this->column_name[$i]==$colName) { $j=$i; }
    }
    if ($j!=-1) { return $this->element[$rowNum][$j];} else { return ""; }
    }
    function getColumn_by_num($rowNum,$colNum) {
    return $this->element[$rowNum][$colNum];
    }
    function getColName($column) {
    return $this->column_name[$column];
    }
    function getNumRows() {
    return $this->numrows;
    }
    function getNumCols() {
    return $this->numcols;
    }
    }
    /*
    ---------------------------------------------------------
    "class ConnInfo" provides a connection details object which returns information
    given on a connection parameter to a database.
    ---------------------------------------------------------
    */
    class ConnectionInfo {
    var $cdbname;
    var $cusername;
    var $cpassword;
    var $chost;
    var $cport;
    function SplitThis($cparam) {
    $tok = strtok($cparam," ");
    while($tok) {
    $pos=strpos($tok,"=");
    $type=substr($tok,0,$pos);
    switch ($type) {
    case "dbname":
    $this->cdbname=substr($tok,$pos+1);
    break;
    case "host":
    $this->chost=substr($tok,$pos+1);
    break;
    case "port":
    $this->cport=substr($tok,$pos+1);
    break;
    case "username":
    $this->cusername=substr($tok,$pos+1);
    break;
    case "password":
    $this->cpassword=substr($tok,$pos+1);
    break;
    }
    $tok = strtok(" ");
    } 
    }
    function dbName() { return $this->cdbname; }
    function Host() { return $this->chost; }
    function Port() { return $this->cport; }
    function UserName() { return $this->cusername; }
    function Password() { return $this->cpassword; }
    }
    /*
    ---------------------------------------------------------
    "class connection" provides functionality to deal with the
    actual database.
    ---------------------------------------------------------
    */
    class connection {
    var $my_connection;
    var $my_temp_resultID;
    var $my_temp_result_object = new resultSet;
    function open($p1, $p2 = "", $p3 = "", $p4 = "", $p5="" ) {
    global $database_type;
    $ok = false;
    if (($p2 == "") && ($p3 == "") && ($p4 == "") && ($p5 == "") && ($database_type==1))
    {
    	$this->my_connection=pg_connect($p1);
    	$ok = true;
    }
    if (($p5 == "") && ($database_type==2))
    {
    $connINF = new ConnectionInfo;
    $connINF->SplitThis($p1);
    $p1=$connINF->Host();
    $p2=$connINF->UserName();
    $p3=$connINF->Password();
    $p4=$connINF->dbName();
    	$this->my_connection=mysql_connect($p1, $p2, $p3);
    	mysql_select_db($p4,$this->my_connection);
    	$ok = true;
    }
    if (!($ok))
    {
    	print "\n\nTHERE WAS AN ERROR CONNECTING.\n\n";
    }
    }	
    function close() {
    global $database_type;
    if ($database_type==1)
    	{
    	pg_close($this->my_connection);
    	}
    if ($database_type==2)
    	{
    	mysql_close($this->my_connection);
    	}
    }
    function runActionQuery($someSQL)
    {
    global $database_type;
    // cleanup SQL for PHP versions! (sic)
    if (substr($someSQL,strlen($someSQL)-1,1)==";") {
    	 $someSQL=substr($someSQL,0,strlen($someSQL)-1);
    	}
    	if ($database_type==1)
    	{
    	pg_exec($this->my_connection, $someSQL);
    	}
    	if ($database_type==2)
    	{
    	mysql_query($someSQL, $this->my_connection);
    	}
    }
    function runSQL($someSQL)	
    {
    	global $database_type;
    // cleanup SQL for PHP versions! (sic)
    if (substr($someSQL,strlen($someSQL)-1,1)==";") {
    	 $someSQL=substr($someSQL,0,strlen($someSQL)-1);
    	}
    	if ($database_type==1)
    	{
    		$this->my_temp_resultID = pg_exec($this->my_connection, $someSQL);
    		$this->my_temp_result_object->numrows = pg_numrows($this->my_temp_resultID);
    		$this->my_temp_result_object->numcols = pg_numfields($this->my_temp_resultID);
    // fill column_names from resultset
    		for ($j=0; $j < $this->my_temp_result_object->numcols; $j++)
    		{
    			$this->my_temp_result_object->column_name[$j] = pg_fieldname($this->my_temp_resultID, $j);
    		}
    // fill data elements from resultset
    		for ($i=0; $i < $this->my_temp_result_object->numrows; $i++)
    		{
    			for ($j=0; $j < $this->my_temp_result_object->numcols; $j++)
    			{
    $this->my_temp_result_object->element[$i][$j] = pg_result($this->my_temp_resultID, $i, $j);
    			}
    		}
    		return $this->my_temp_result_object;
    pg_freeresult($this->my_temp_resultID);
    	}
    if ($database_type==2)
    	{
    	$this->my_temp_resultID = mysql_query($someSQL, $this->my_connection);
    	$this->my_temp_result_object->numrows = mysql_num_rows($this->my_temp_resultID);
    	$this->my_temp_result_object->numcols = mysql_num_fields($this->my_temp_resultID);
    // fill column_names from resultset
    	for ($j=0; $j < $this->my_temp_result_object->numcols; $j++)
    	{
    		$this->my_temp_result_object->column_name[$j] = mysql_fieldname($this->my_temp_resultID, $j);
    	}
    // fill data elements from resultset
    	for ($i=0; $i < $this->my_temp_result_object->numrows; $i++)
    		{
    		$x = mysql_fetch_row($this->my_temp_resultID);
    		for ($j=0; $j < $this->my_temp_result_object->numcols; $j++)
    			{
    			$this->my_temp_result_object->element[$i][$j] = $x[$j];
    			}
    		}
    	return $this->my_temp_result_object;
    	mysql_free_result($this->my_temp_resultID);
    	}
    	}
    } 
    ?>


Other 198 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this code(in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments

 There are no comments on this submission.
 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | PHP Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.