Query in PHP with 'and' or 'or' as variable

52 Views Asked by At

I would like to do search for advanced search. The Search feature has and/or for every category. User can choose any combination of and n or. Here i give the screenshot

Advanced Search

I store the and/or into variable call $pil, $pil1,$pil2 and $pil3. And will put them in query. it's better than validate one by one every condition of and/or

So this is my query using postgresql in PHP

$query = pg_query("SELECT   evaluationdate,onlinename,channel,topik,reviewername,sourceevaluation,evaluation 
from AgentPerformance 
where onlinename like '%".$VEOn1."%'
".$pil." reviewername like '%".$VERev1."%' 
".$pil1." channel like '%".$VEChan1."%' 
".$pil2."sourceevaluation like '%".$VESource1."%'
".$pil3."evaluationdate between '".$VEStart1."' and '".$VEEnd1."'");

EDIT :

The problem now, All the variables must not be empty or the query will be error. any way to trick this?

2

There are 2 best solutions below

2
On BEST ANSWER

You've missed some spaces near sourceevaluation and evaluationdate
Try with this query :

$query = pg_query("SELECT   evaluationdate,onlinename,channel,topik,reviewername,sourceevaluation,evaluation 
from AgentPerformance 
where onlinename like '%".$VEOn1."%'
".$pil." reviewername like '%".$VERev1."%' 
".$pil1." channel like '%".$VEChan1."%' 
".$pil2." sourceevaluation like '%".$VESource1."%'
".$pil3." evaluationdate between '".$VEStart1."' and '".$VEEnd1."'");
1
On

Simply. Use validation for each $pil whether it is empty or not. it makes me validate 4 times, but it solves the problem. The syntax error has been solved too