phppostgresqlsearchpg-query

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


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?


Solution

  • 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."'");