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;
	}

}

Recommended Answers

All 2 Replies

It is a pretty good start at OOP however I would suggest to you that as a general rule of thumb with OOP methods are named in CamelCase format ie.,

class someClass
{
  private $some_property;
  
  public function someMethod()
  {}
}

So properties(fields in Java) are written in underscore format some_property and methods are written in CamelCase public function someMethod...

i would add more debugging features, like having it echo out a specific table or the entire database. it makes it easier to make sure information was added to the database correctly, rather than having to check a table from the command line or phpMyAdmin. It has worked well. Make sure it can't be used when the site is live though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.