Question About Database Abstraction Class

I am building a basic database class in PHP that I can pass just about any query to… kind of like a CRUD but it’s just for MySQL SELECT Statements right now. The way I have the query_string now is:

$this->_queryString = sprintf(“SELECT $this->_fields FROM $this->_table WHERE $this->_where ORDER BY $this->_orderby LIMIT $this->_limit”);

$this->_query = mysql_query($this->_queryString) or die(mysql_error());

My question is how do I handle empty values for WHERE, ORDER BY or LIMIT? Not all queries have these values. Is there some sort of empty() function I can run inline with my SQL code?

  1. Since my PHP Class is expecting 5 values (fields, table, where, order_by and limit)… I have to pass something to the class or it errors out. So how do I not run say ORDER BY if the value comes through as empty for that? If ORDER BY is sent over as “” I just want to run this:

$this->_queryString = sprintf(“SELECT $this->_fields FROM $this->_table WHERE $this->_where LIMIT $this->_limit”);

Here’s my full class:


class DBQuery {
	
	public function setQuery($_post_fields,$_post_table,$_post_where,$_post_orderby,$_post_limit) {
		$this->_fields = $_post_fields;
		$this->_table = $_post_table;
		$this->_where = $_post_where;
		$this->_orderby = $_post_orderby;
		$this->_limit = $_post_limit;

		$this->_queryString = sprintf("SELECT $this->_fields FROM $this->_table WHERE $this->_where LIMIT $this->_limit");
		
		$this->_query = mysql_query($this->_queryString) or die(mysql_error());
			
		$this->_num_rows = mysql_num_rows($this->_query);
		
		if ($this->_num_rows < 1) {
			throw new Exception('This Category Has No Posts!');
		}
		else {
			$this->list = array();
			
			while($this->results = mysql_fetch_array( $this->_query )) {
				$this->list[] = $this->results;
			}
		}
		
	}
	
	public function getQuery() {
		return $this->list;
	}
	
	public function getNumRows() {
		return $this->_num_rows;
	}
	
}

Can I set default values in the function like:

public function setQuery($_post_fields,$_post_table,$_post_where=“”,$_post_orderby=“”,$_post_limit=“”)

and then use an if statement to check that?