Abstraction Layers
|
|
|
|
<?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);
}
}
?>
|
|
|
Usage Example
|
<?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";
} ?>
|
|
|
Rate This Script
|
|
|
|