this class is dead i have replaced it with a more up to date class
with PDO and some less features. This is a work in progress to replace my mysql class.
please this class is a work in progress so some things will change or taken away. and features not needed in this version please let me know or features it's missing.
class DBO{
const ERR_SILENT = 0;
const ERR_WARNING = 1;
const ERR_EXCEPTION = 2;
const FETCH_ASSOC = 3;
const FETCH_BOTH = 4;
const FETCH_OBJ = 5;
const FETCH_NUM = 6;
const FETCH_CLASS = 7;
private $_error_mode = 2;
private $_error_last = '';
private $_query_last = '';
private $_results_last = '';
private $_affected_rows = 0;
private $_columns_count = 0;
private $_query_count = 0;
private $_results_count = 0;
private $_tables = array();
private $_columns = array();
private $_statements = array();
private $_connect_persitent = false;
public function __construct($driver = '' , $dbname = '' , $server = '' , $user = '' , $pass = ''){
$this->_params = array($driver , $dbname , $server , $user , $pass);
foreach($this->_params as $params){
if(!is_string($params)){
if($this->_error_mode === 0){
return false;
}elseif($this->_error_mode === 1){
trigger_error('Argument Error: seems one or more of the supplied arguments is not a '
.'string type only string types are expected by DB __construct. File:');
}else{
throw new DBO_Exception('Argument Error: seems one or more of the supplied arguments is not a '
.'string type only string types are expected by DB __construct. File:');
}
}
}
$this->connect($this->_params[0], $this->_params[1], $this->_params[2],$this->_params[3],$this->_params[4]);
}
protected function connect($driver , $dbname , $server , $user , $pass){
if(strtolower($driver) === 'mysql'){
try{
$this->_lid = new PDO($driver.':host='.$server.';dbname='.$dbname , $user , $pass);
}catch(PDOException $pdo_error){
if($this->_error_mode === 0){
return false;
}elseif($this->_error_mode === 1){
trigger_error('Database Error: '. $pdo_error->getMessage());
$this->_error_last = array(
'message' => $pdo_error->getMessage() ,
'file' => __FILE__,
'line' => __LINE__,
);
}else{
throw new DBO_Exception('Database Error: '. $pdo_error->getMessage());
}
}
}elseif(strtolower($driver) === 'pgsql'){
try{
$this->_lid = new PDO($driver.':dbname='.$dbname.';host='.$server, $user , $pass);
}catch(PDOException $pdo_error){
if($this->_error_mode === 0){
return false;
}elseif($this->_error_mode === 1){
trigger_error('Database Error: '. $pdo_error->getMessage());
}else{
throw new DBO_Exception('Database Error: '. $pdo_error->getMessage());
}
}
}
return $this->_lid;
}
public function query($statement = '' , array $params = array()){
if(count($params) > 0){
return $this->prepare($statement , $params)->execute();
}else{
return $this->prepare($statement)->execute();
}
return false;
}
public function prepare($statement = '' , array $params = array()){
if(!is_string($statement)){
if($this->_error_mode === 0){
return false;
}elseif($this->_error_mode === 1){
trigger_error('Argument Error: seems one or more of the supplied arguments is not a '
.'string type only string types are expected by DB prepare. File:');
}else{
throw new DBO_Exception('Argument Error: seems one or more of the supplied arguments is not a '
.'string type only string types are expected by DB prepare. File:');
}
}
if(!is_array($params)){
if($this->_error_mode === 0){
return false;
}elseif($this->_error_mode === 1){
trigger_error('Argument Error: seems one or more of the supplied arguments is not a '
.'string type only string types are expected by DB prepare.');
}else{
throw new DBO_Exception('Argument Error: seems one or more of the supplied arguments is not a '
.'string type only string types are expected by DB prepare.');
}
}
$this->_query_count++;
$qid = $this->_query_count;
$this->_statements[$qid] = array($statement , $params);
$this->_statements;
return $this;
}
public function execute(){
$statement = $this->_statements[$this->_query_count][0];
$params = $this->_statements[$this->_query_count][1];
if(count($params) > 0){
$transaction = $this->_lid->prepare($statement);
$transaction->execute($params);
$this->_columns_count = $transaction->columnCount();
$this->_affected_rows = $transaction->rowCount();
}else{
$transaction = $this->_lid->prepare($statement);
$transaction->execute();
$this->_columns_count = $transaction->columnCount();
$this->_affected_rows = $transaction->rowCount();
}
$this->_statements[$this->_query_count] = $transaction;
return $this;
}
public function insert($table , array $columns = array() , array $values = array()){
$qs = "INSERT INTO ".$table;
$qs.= "('".implode("','" , $columns)."')";
$qs.= "VALUES('".implode("','" , $values)."')";
$qs.= ";";
$query = $this->prepare($qs)->execute();
}
public function fetch($fetch_mode = self::FETCH_BOTH){
$transaction = $this->_statements[$this->_query_count];
$fetch_modes = array(
3 => PDO::FETCH_ASSOC,
4 => PDO::FETCH_BOTH ,
5 => PDO::FETCH_OBJ ,
6 => PDO::FETCH_NUM ,
7 => PDO::FETCH_CLASS
);
$results = $transaction->fetch($fetch_modes[$fetch_mode]);
$this->_results_count = count($results);
return $results;
}
public function fetchAll(){
$transaction = $this->_statements[$this->_query_count];
$results = $transaction->fetchAll();
$this->_results_count = count($results);
return $results;
}
public function fetchObject(){
$transaction = $this->_statements[$this->_query_count];
$results = $transaction->setFetchMode(PDO::FETCH_OBJECT);
return $results;
}
public function fetchClass($classname , array $args = array()){
$transaction = $this->_statements[$this->_query_count];
if(!is_array($args)){
if($this->_error_mode === 0){
return false;
}elseif($this->_error_mode === 1){
trigger_error('Argument Error: seems one or more of the supplied arguments is not a '
.'string type only string types are expected by DB prepare.');
}else{
throw new DBO_Exception('Argument Error: seems one or more of the supplied arguments is not a '
.'string type only string types are expected by DB prepare. File:');
}
}elseif(count($args) > 0){
$results = $transaction->setFetchMode(PDO::FETCH_CLASS , (string)$classname , (array) $args);
}else{
$results = $transaction->setFetchMode(PDO::FETCH_CLASS , (string)$classname);
}
return $results;
}
public function getNumColumns(){
return $this->_columns_count;
}
public function getNumResults(){
return $this->_results_count;
}
public function getAffectedRows(){
return $this->_affected_rows;
}
public function getLastError(){
return $this->_last_error;
}
public function getErrorMode(){
return $this->_error_mode;
}
public function queryCount(){
return $this->_query_count;
}
public function setAttributes(){
$args = func_get_args();
foreach($args as $arg){
$this->setPersistentConnection($arg);
$this->setErrorMode($arg);
}
return;
}
public function setErrorMode($error_mode){
if(is_int($error_mode)){
if($error_mode === 0 || $error_mode === 1 || $error_mode === 2){
return $this->_error_mode = (int)$error_mode;
}
}
return false;
}
}
class DBO_Exception extends Exception{
}
try{
$db = new DBO('mysql' , 'testdb' , 'localhost' , 'user' , 'pass');
var_dump($db->insert('users' , array('firstname' , 'lastname') , array('josh' , 'getner')));
$db->prepare('SELECT * FROM users')->execute();
$res = $db->fetch(DBO::FETCH_ASSOC);
var_dump($res);
}catch(DBO_Exception $dboerror){
echo $dboerror->getMessage();
}