<?php
#===========================================================================
#= Script : Database Access Class
#= File : example_MySQL.php
#= Version: 0.2
#= Author : Mike Leigh
#= Email : hide@address.com
#= Website: http://www.mikeleigh.com/scripts/databaseaccess
#= Support: http://www.mikeleigh.com/forum
#===========================================================================
#= Copyright (c) 2005 Mike Leigh
#= You are free to use and modify this script as long as this header
#= section stays intact
#= This file is part of DownloadCounter.
#=
#= This program is free software; you can redistribute it and/or modify
#= it under the terms of the GNU General Public License as published by
#= the Free Software Foundation; either version 2 of the License, or
#= (at your option) any later version.
#=
#= This program is distributed in the hope that it will be useful,
#= but WITHOUT ANY WARRANTY; without even the implied warranty of
#= MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#= GNU General Public License for more details.
#=
#= You should have received a copy of the GNU General Public License
#= along with DownloadCounter; if not, write to the Free Software
#= Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#===========================================================================
class phpDB {
var $attributes = array('status' => true);
function phpDB($attributes = array()) {
if(count($attributes) >= 1) {
foreach($attributes as $key => $value) {
$this->setAttribute($key, $value);
}
}
$defaults = array();
$defaults['fetch'] = 'associative';
foreach($defaults as $key => $value) {
if(!array_key_exists($key, $this->attributes)) {
$this->setAttribute($key, $value);
}
}
}
function getAttribute($attribute) {
if($this->getStatus() == true) {
if(array_key_exists($attribute, $this->attributes)) {
return $this->attributes[$attribute];
} else {
$this->setStatus(false);
$this->setMessage("Attribute: '".$attribute."' does not exist");
return $this->getStatus();
}
}
}
function setAttribute($attribute, $value) {
if($this->getStatus() == true) {
$this->attributes[$attribute] = $value;
}
}
function getStatus() {
return $this->attributes['status'];
}
function setStatus($value) {
$this->attributes['status'] = $value;
}
function getMessage() {
return $this->attributes['message'];
}
function setMessage($value) {
$this->attributes['message'] = $value;
}
function getResultArray() {
if($this->getStatus() == true) {
if(array_key_exists('result', $this->attributes)) {
return $this->attributes['result'];
} else {
$this->setStatus(false);
$this->setMessage("Attribute: '".$attribute."' does not exist");
return $this->getStatus();
}
}
}
function connect() {
$type = $this->getAttribute('type');
if($type <> false) {
switch($type) {
case 'MSSQL';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break;
case 'MySQL';
$this->setAttribute('link', mysql_connect($this->getAttribute('host'), $this->getAttribute('username'), $this->getAttribute('password')));
break;
case 'ODBC';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break;
case 'Oracle';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break;
case 'PostgreSQL';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break;
case 'Sybase';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break;
default:
$this->setMessage("Database type:".$type." is invalid");
$this->setStatus(false);
}
}
}
function close() {
$type = $this->getAttribute('type');
if($type <> false) {
switch($type) {
case 'MSSQL';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break;
case 'MySQL';
mysql_close($this->getAttribute('link'));
break;
case 'ODBC';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break;
case 'Oracle';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break;
case 'PostgreSQL';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break;
case 'Sybase';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break; default:
$this->setMessage("Database type:".$type." is invalid");
$this->setStatus(false);
}
}
}
function selectDatabase() {
$type = $this->getAttribute('type');
if($type <> false) {
switch($type) {
case 'MSSQL';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break;
case 'MySQL';
$result = mysql_select_db($this->getAttribute('database'), $this->getAttribute('link'));
if($result <> false) {
if($result == false) {
$this->setMessage(mysql_error($this->getAttribute('link')));
$this->setStatus(false);
}
}
break;
case 'ODBC';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break;
case 'Oracle';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break;
case 'PostgreSQL';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break;
case 'Sybase';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break; default:
$this->setMessage("Database type:".$type." is invalid");
$this->setStatus(false);
}
}
}
function fetch($sql) {
$type = $this->getAttribute('type');
if($type <> false) {
switch($type) {
case 'MSSQL';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break;
case 'MySQL';
$status = $this->execute($sql);
if($status == true) {
$numrows = mysql_num_rows($status);
$fetch_type = $this->getAttribute('fetch');
switch($fetch_type) {
case 'associative';
$fetch = MYSQL_ASSOC;
break;
case 'numeric';
$fetch = MYSQL_NUM;
break;
case 'both';
$fetch = MYSQL_BOTH;
break;
default:
$this->setMessage("attribute['fetch'] must be either 'associative', 'numeric' or 'both' e.g. object->setAttribute['fetch'] = 'numeric';");
$this->setStatus(false);
}
if($this->getAttribute('status') == true) {
$this->setAttribute('result', array());
for($i = 0; $i <= $numrows - 1; $i++) {
$this->attributes['result'][$i] = mysql_fetch_array($status, $fetch);
}
}
}
break;
case 'ODBC';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break;
case 'Oracle';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break;
case 'PostgreSQL';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break;
case 'Sybase';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break; default:
$this->setMessage("Database type:".$type." is invalid");
$this->setStatus(false);
}
}
}
function execute($sql) {
$type = $this->getAttribute('type');
if($type <> false) {
switch($type) {
case 'MSSQL';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break;
case 'MySQL';
$status = mysql_query($sql, $this->getAttribute('link'));
if($status == false) {
$this->setMessage(mysql_error($this->getAttribute('link')));
$this->setStatus(false);
}
break;
case 'ODBC';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break;
case 'Oracle';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break;
case 'PostgreSQL';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break;
case 'Sybase';
$this->setMessage("Database type:".$type." has not yet been provided for in this class");
$this->setStatus(false);
break; default:
$this->setMessage("Database type:".$type." is invalid");
$this->setStatus(false);
}
return $status;
}
}
}
?>