Hi there, I'm looking for some feedback on a simple database class i've written. I'm just trying to get to grips with OOP, so any feedback would be appreciated. I do realise that PDO is available, and that this database class is really just a wrapper for mysql_* functions, but still it's just for learning purposes.
database.php
class Database
{
private $config = null;
private $link = null;
private $db = null;
public $error;
/**
* Database::__construct()
*
* @return
*/
public function __construct(Config $conf)
{
$this->set_config($conf)
->connect()
->select_db();
}
/**
* Database::set_config()
*
* @return
*/
public function set_config($conf)
{
$this->config = $conf;
return $this;
}
/**
* Database::connect()
*
* @return
*/
protected function connect()
{
$this->link = mysql_connect($this->config->hostname, $this->config->username, $this->config->password);
if ( ! $this->link)
{
$this->error = mysql_error();
return $this->error();
}
return $this;
}
/**
* Database::select_db()
*
* @return
*/
protected function select_db()
{
$this->db = mysql_select_db($this->config->database, $this->link);
if ( ! $this->db)
{
$this->error = mysql_error();
return $this->error();
}
}
/**
* Database::__destruct()
*
* @return
*/
public function __destruct()
{
mysql_close($this->link);
}
/**
* Database::query()
*
* @return
*/
public function query($sql)
{
if ($sql)
{
$resource = mysql_query($sql);
if (mysql_error())
{
$this->error = mysql_error();
return $this->error();
}
else
{
return $resource;
}
}
else
{
$this->error = "No query supplied";
return $this->error();
}
}
/**
* Database::fetch_rows()
*
* @return
*/
public function fetch_array($result)
{
$rows = array();
if ($result)
{
while ($row = mysql_fetch_array($result))
{
$rows[] = $row;
}
return $rows;
}
else
{
return $this->error();
}
}
/**
* Database::fetch_row()
*
* @param mixed $result
* @return
*/
public function fetch_row($result)
{
if ($result)
{
$row = mysql_fetch_row($result);
if ($row)
{
return $row;
}
else
{
$this->error = mysql_error();
return $this->error();
}
}
else
{
return $this->error();
}
}
/**
* Database::count_rows()
*
* @return
*/
public function count_rows($result)
{
if ($result)
{
return mysql_num_rows($result);
}
else
{
$this->error = mysql_error();
return $this->error();
}
}
/**
* Database::get_db()
*
* @return
*/
public function get_db()
{
return $this->_db;
}
/**
* Database::error()
*
* @return
*/
private function error()
{
echo "Please contact the website administrator. ".$this->error;
die;
}
}