I needed a quick way to switch quickly between MySQL and MS SQL Server for applications, so I wrote two classes, that provide a modular, consistent API that can be used for either database. Changing out your DB is as easy as dropping in the proper DB_whatever.php file into the dir that DB.php lives in, then changing the $dbtype var before you require() DB.php
By : jcostom
<?php
/*
Copyright (c) 2000, Jason Costomiris
All rights reserved.
Don't be scared, it's just a BSD-ish license.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software developed by Jason Costomiris.
4. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* This section should be in a file named DB.php */
class DB_base {
// get server/instance
function setinst($instance){
$this->instance = $instance;
}
// get username
function setuser($user){
$this->user = $user;
}
// get password
function setpass($pass){
$this->pass = $pass;
}
// get database name
function dbname($dbname){
$this->dbname = $dbname;
}
// Use persistent connections?
// Only call this if you want persistent connections
function persist(){
$this->persist = 1;
}
}
$file = join("", array("DB_", $dbtype, ".php"));
require($file);
?>
<?php
/* This section should be in a file named DB_mysql.php */
class DB extends DB_base {
// Connect or pconnect.
function connect(){
if($this->persist){
$this->conn =
mysql_pconnect($this->instance, $this->user, $this->pass);
} else {
$this->conn =
mysql_connect($this->instance, $this->user, $this->pass);
}
mysql_select_db($this->dbname, $this->conn);
return($this->conn);
}
// close
function close(){
if($this->persist) {
$ret = 1;
} else {
$ret = mysql_close($this->conn);
}
return($ret);
}
// query
function query($query){
$this->result = mysql_query($query);
return($this->result);
}
// numrows
function numrows(){
$this->numrows = mysql_num_rows($this->result);
return($this->numrows);
}
// affected rows
function affrows(){
$this->affrows = mysql_affected_rows($this->result);
return($this->affrows);
}
// seek
function seek($row){
$seek = mysql_data_seek($this->result, $row);
return($seek);
}
// fetch object
function fobject(){
$object = mysql_fetch_object($this->result);
return($object);
}
// fetch array
function farray(){
$array = mysql_fetch_array($this->result);
return($array);
}
// free
function free(){
$free = mysql_free_result($this->result);
return($free);
}
}
?>
/* example usage
<?php
$dbtype = "mysql";
require("DB.php");
$db = new DB;
$db->setinst("dbserver");
$db->setuser("dbusername");
$db->setpass("dbpassword");
$db->dbname("dbname");
$db->persist();
$conn = $db->connect();
$db->query("SELECT id,narf FROM foo WHERE id > 4");
while($r = $db->fobject()){
echo "$r->id: $r->narf<br>\n";
}
?>
*/
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Database Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
Learn to enable users to both rate existing animations and to combine existing animations into new snippets. This is the third in a series of three tutorials that chronicle the building of a site that enables collaborative discussion and animation building using Domino and OpenLaszlo. FREE! Go There Now!
|
|
|
|
In this webcast, you'll get an introduction to the eXtreme Transaction Processing (XTP) features of WebSphere Extended Deployment and the common architectural traits required by XTP applications. See how WebSphere Extended Deployment's ObjectGrid feature provides a state-of-the-art infrastructure for hosting XTP applications. FREE! Go There Now!
|
|
|
|
This whitepaper provides areas to consider when evaluating any software configuration management solution. It addresses how the IBM solutions (Rational ClearCase and Rational ClearQuest) meet the needs and requirements of both project leaders and developers to provide successful Software Change and Configuration Management. FREE! Go There Now!
|
|
|
|
Effective governance for lean development isn’t about command and control. Instead, the focus is on enabling the right behaviors and practices through collaborative and supportive techniques. Hear from Scott Ambler on how it is far more effective to motivate people to do the right thing than it is to force them to do so. Learn how to form a lightweight, collaboration-based framework that reflects the realities of modern IT organizations. FREE! Go There Now!
|
|
|
|
Building a software-as-a-service solution requires addressing a few key technical challenges. In this webcast, we'll focus on the role of IBM Tivoli Directory Server and WebSphere Portlet Factory in creating a Software as a Service solution. We will demonstrate how to use Tivoli Directory Server to prevent the user population of one tenant from accessing the virtual portal and portlet components of another tenant. We will also use the dynamic profile capability of WebSphere Portlet Factory to create multiple highly customized applications from one code base. FREE! Go There Now!
|
|
|
|
The unprecedented scope of a service-oriented architecture (SOA) initiative brings to the forefront a number of management and governance issues that were sidestepped in the past. The key to a successful SOA implementation is managing and governing activities throughout the entire SOA delivery lifecycle by ensuring that services conform to the needs of all of the business’s stakeholders. Learn how service lifecycle management allows the business to ensure that the process by which services are defined, created, tested, deployed, optimized and retired is manageable, repeatable and auditable. FREE! Go There Now!
|
|
|
|
As organizations integrate software into every aspect of business, they are constantly pressured to deliver faster, better, and cheaper results. Unfortunately, a “dis-integrated” software delivery approach reduces returns while increasing costs. This IBM Rational White Paper shows how Integrated Requirements Management aligns organizations around maximizing value and keeping pace with change. FREE! Go There Now!
|
|
|
|
You'll get answers to many questions and more from David Barnes, Lead Evangelist for IBM Emerging Internet Technologies. David will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download IBM DB2 Express-C 9.5, a no-charge version of DB2 Express 9 database server. DB2 Express-C offers the same core data server base features as other DB2 Express editions and provides a solid base to build and deploy applications developed using C/C++, Java, .NET, PHP, and other programming languages. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered! FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |