<?php
/**
* @name MYSQL Database Abstraction Layer
* @author Joey Adams
* @see http://bin.joeyadams.net/mysql_ab_pro/
* @version 4.0
* @uses
* Use this Abstraction layer to keep your business logic seperate.
*
* Change Options Below for either mysql or mysqli drivers.
*
* By Default query() returns Associative array, change options to return resource, or object.
*
* Note: On error, function db::error($str) is called, which just die()'s with error string.
* Modify this to log errors, or turn off for silence, or whatever you like. Change Error in
* config for silence.
*
* Free to use and modify to your liking, just keep this comment block here.
*
* Make sure you run your vars through prepare() before using them in query() to protect
* against SQL Injection.
*
*
* When performing query() , it accepts 2 arguments. query($string,$return_type);
* String is the query string. Return type is one of the below;
* ASSOS = Associative Array
* OBJ = Return Object
* RES = return resource
*
* If no type is given, it will return type per config.
*
*
* @copyright GPL (http://www.gnu.org/licenses/gpl.txt)
* 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 3 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 this program. If not, see <http://www.gnu.org/licenses/>
*
*/
class DB {
// Options
/**
* Set MYSQL Driver
* 0 = MYSQL
* 1 = MYSQLI
*
* @var int driver
*/
private $driver = 1;
/**
* Default Query Results Return Style
*
* 0 = Return Resource
* 1 = Return Associative Array
* 2 = Return Object
*
* @var int def_return_style
*/
private $def_return_style = 1;
/**
* Log File to store Error info
* Make sure this file is readable/writeable
*
* @var str log_file
*/
private $log_file = "mysql_pro_error.log";
/**
* Log Erros
* 0 = Do not log errors
* 1 = Log Errors
*
* Logs errors to log_file above
*
*/
private $log_errors = 0;
/**
* Set whether to die() with error string
*
* 0 = Do not display, silent mode.
* 1 = die() with errors.
*
* Note: Change db::error() to change how
* errors are handled
*
* @var unknown_type
*/
private $display_errors = 0;
//-- End Options --
// Connection Info
/**
* MYSQL Server Host
* @var str host
*/
private $host = "host";
/**
* MYSQL Username
* @var str username
*/
private $username = "username";
/**
* MYSQL Password
* @var str password
*/
private $password = "password";
/**
* MYSQL Default Database
* Set this to default db to use, although you can
* pass a db name to getInstance() to use different one
*
* @var str def_db
*/
private $def_db = "test";
//-- End Connection Info --
/**
*
*
* Edit Below This line at your own risk
*
*
*/
/**
* Variable holds plugin for iObject
*
* @var str class_name
*/
private $iobj;
/**
* Variables holds plugin for iRow
*
* @var unknown_type
*/
private $irowobj;
/**
* MYSQL Connection Link
* Holds Link for connection for functionality
*
* @var static MYSQL_CONNECTION
*/
/**
* Array that stores connections
* made with create()
*
* @var array
*/
private
$_link_array =
array();
/**
* Establish MYSQL Connection
* with either mysql, or mysqli driver
* based on config.
*
* Accepts 1 param for database name,
* If null, uses self::def_db
*
* @param str $db
*/
public function __construct($db = null){
$this->def_db = $db;
}
if($this->driver){
$this->_link = new mysqli($this->host,$this->username,$this->password,$this->def_db);
if(mysqli_connect_errno()){
$this->error(mysqli_connect_error());
}
} else {
$this->_link =
mysql_connect($this->
host,
$this->
username,
$this->
password);
}
}
$this->_link_array['def'] = $this->_link;
}
/**
* Closes MYSQL Connection
*
*/
public function __destruct(){
if($this->driver){
$this->_link->close();
} else {
}
}
/**
* Add extended class plugin. Extends iObject return style.
*
* @param string $class_name
*/
public function iobjectPlugin($class_name){
$this->iobj = $class_name;
}
}
/**
* Add extended class plugin. Extends iRow for iObject return style.
*
* @param string $class_name
*/
public function irowPlugin($class_name){
$this->irowobj = $class_name;
}
}
/**
* Called when error occurs
*
* @param str $error
*/
public function error($error){
if($this->log_errors){
if(!
$fp =
fopen($this->
log_file,
'a')){
if($this->display_errors){
die("Error Handler: Could not open file for writing, ".
$this->
log_file);
}
}
$date =
date(DATE_RFC822
);
$err = $date . " - " . $error . "\n";
if($this->display_errors){
die("Error Handler:Could Not Write To File");
}
}
}
if($this->display_errors){
}
}
/**
* Returns instance of MYSQL
*
* Use this to init DB(), or to
* get an instance of the connection.
*
* Makes sure only 1 persistent connection
* is maintained.
*
* Pass a string when initializing if you want
* to use a different database than in config.
*
* @param str $db
* @return MYSQL_CONNECTION
*/
public function getInstance($db = null){
self::$_link = new DB($db);
}
return self::$_link;
}
/**
* Wrapper for Real_Escape_String
*
* Accepts either array, or string
* and returns escaped vars.
*
* Use this before query() on vars to
* protect from sql injection
*
* @param str,array $var
* @return str,array $clean
*/
public function prepare($var){
if($this->driver){
foreach($var as $key=>$val){
$clean[$key] = $this->_link->real_escape_string($val);
}
} else {
foreach($var as $key=>$val){
}
}
} else {
if($this->driver){
$clean = $this->_link->real_escape_string($var);
} else {
}
}
return $clean;
}
/**
* Performs Query Against Database
*
* Accepts two args, the query string,
* and an optional return style.
*
* Return Style Can be
* 'OBJ' or 'OBJECT' = Return Object
* 'RES' or 'RESOURCE' = Return Resource
* 'ASSOC' or 'ASSOCIATIVE' = Return Associative Array
*
* Note:: If return_style is omitted, return will be determined
* by config above.
*
* @param str $query
* @return MYSQL_RESULT,ASSOC_ARRAY
*/
public function query($query,$return_style = null){
$return_style = $this->_getReturnStyle($return_style);
if($this->driver){
if(!$resultSet = $this->_link->query($query)){
$this->error(mysqli_error($this->_link));
}
if($return_style){
if($return_style == 1){
$returnResult['affected_rows'] = $this->_link->affected_rows;
$returnResult['num_rows'] = $resultSet->num_rows;
if($resultSet->num_rows){
$i=0;
while($result = $resultSet->fetch_assoc()){
foreach($result as $key=>$val){
$returnResult[$i][$key] = $val;
}
$i++;
}
}
}
} elseif ($return_style == 2){
$i_obj = $this->iobj;
} else {
$i_obj = "iobject";
}
$iobject = new $i_obj($this->irowobj);
$iobject->query = $query;
$iobject->affected_rows = $this->_link->affected_rows;
$iobject->num_rows = $resultSet->num_rows;
if($resultSet->num_rows){
while($result = $resultSet->fetch_assoc()){
$row = $iobject->newRow();
foreach($result as $key=>$val){
$row->$key = $val;
}
}
}
}
$returnResult = &$iobject;
}
} else {
$returnResult = $resultSet;
}
} else {
}
if($return_style){
if($return_style == 1){
if($returnResult['num_rows']){
$i=0;
foreach($result as $key=>$val){
$returnResult[$i][$key] = $val;
}
$i++;
}
}
}
} elseif ($return_style == 2){
if