Hey guys i started to write DB class and though i could maybe get some feedback on it so far.
Remember its still in the works...
<?php
//DB Classes
abstract class DB
{
protected abstract function connect();
public abstract function query($query);
public abstract function close();
public abstract function selectDB();
}
class Mysql_DB extends DB
{
private $host;
private $user;
private $pass;
private $db;
private $dbcx;
private $error = array();
public function __construct($host,$user,$pass,$db)
{
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->db = $db;
$this->connect();
}
protected function connect()
{
$connect = @mysql_connect($this->host,$this->user,$this->pass);
if (!$connect)
{
$this->error['Connection'] = 'Connection could not be made at host: '.$this->host.' --- MYSQL ERROR: '.mysql_error($this->dbcx);
return false;
} else {
$this->dbcx = $connect;
if ($this->selectDB())
{
return true;
}
}
}
public function selectDB()
{
$select = @mysql_select_db($this->db,$this->dbcx);
if (!$select)
{
$this->error['Database'] = 'Database '.$this->db.' doesnt exist --- MYSQL ERROR: '.mysql_error($this->dbcx);
return false;
} else {
return true;
}
}
public function query($query)
{
if (!$result = @mysql_query($query,$this->dbcx))
{
$this->error['Query'] = 'Error with Query:\''.$query.'\' --- MYSQL ERROR: '.mysql_error($this->dbcx);
return false;
} else {
return $result;
}
}
public function affectedRows()
{
return mysql_affected_rows($this->dbcx);
}
public function characterSet()
{
return mysql_client_encoding($this->dbcx);
}
public function dataSeek($row)
{
if (@mysql_data_seek($this->dbcx,$row))
{
return true;
} else {
$this->error['DataSeek'] = 'Failure moving pointer to row '.$row.' --- MYSQL ERROR: '.mysql_error($this->dbcx);
return false;
}
}
public function setResultType($type)
{
$this->Result_Type = $type;
}
public function fetchArray($results,$result_type = 'ASSOC')
{
switch ($result_type)
{
case 'ASSOC':
$result_type = MYSQL_ASSOC;
break;
case 'NUM':
$result_type = MYSQL_NUM;
break;
case 'BOTH':
$result_type = MYSQL_BOTH;
break;
default:
$result_type = MYSQL_ASSOC;
break;
}
$returnArray =@mysql_fetch_array($results,$result_type);
if ($returnArray)
{
return $returnArray;
} else {
$this->error['FetchArray'] = 'Unable to fetch array --- MYSQL ERROR: '.mysql_error($this->dbcx);
return false;
}
}
public function close()
{
if (@mysql_close($this->dbcx))
{
return true;
} else {
$this->error['Close DB'] = $this->host.'Failed to close --- MYSQL ERROR:'.mysql_error($this->dbcx);
return false;
}
}
public function isError()
{
if (empty($this->error))
{
return false;
} else {
return true;
}
}
public function getError()
{
$errors ='';
foreach ($this->error as $cause => $error)
{
$errors .= $cause.' ~~ '.$error.'<br />';
}
return $errors;
}
public function __destruct()
{
$this->close();
}
}
?>