Building a query
Posted: Sun Jan 24, 2010 8:57 am
Ok its high time for me to deal with this.Its not necessary a php question - its more like general programming matter. What is the best strategy for creating complex database queries. Lets have for example a page and its content can be filtered by a few criteria. The building of the query string could turn into nightmare when more filters exist. The code begins to look something like this(pseudo(php)code):
.....and s.o.
So what is the right way to build complex queries depending on many parameters to avoid situations like the one above?
Code: Select all
$q = "SELECT * FROM articles";
$where = "";
if($filter1!=="All"){
$where.="WHERE author=".$filter1;
}
if($filter2!=="All"){
if($where!==""){
$where.="WHERE category=".$filter2;
}else{
$where.= " AND category=".$filter2;
}
}
So what is the right way to build complex queries depending on many parameters to avoid situations like the one above?