title: How do I build a dynamic SQL query?
body: <p>Right, so I have a set of dropdowns on my page. Depending on whether a value is selected, I want to add it to an SQL query string in PHP. Example:</p>
  
  <pre><code>select1: options("*" "op1", "op2)
  select2: options("*" "op1", "op2)
  select3: options("*" "op1", "op2)
  </code></pre>
  
  <p>'*' refers to anything. i.e the data should not be filtered by that query option. Now, how do I build the query for this quickly and simply? Currently, I have something like this:</p>
  
  <pre><code>$query='';
  $found=0;
  $op1=$_POST['select1'];
  $op2=$_POST['select2'];
  $op3=$_POST['select3'];
  
  if($op1!='*')
  {
  $found=1;
  $op1="'".$op1."'";
  $query="WHERE op1=$op1 ";
  
  }
  
  if($op2!='*')
  {$op2="'".$op2."'";
  if($found==1)
      {
      $query=$query. "AND op2=$op2 ";
      }
  else{
      $found=1;
      $query="WHERE op2=$op2 ";
      }
  }
  
  if($op3!='*')
  {$op3="'".$op3."'";
  if($found==1)
      {
      $query=$query. "AND op3=$op3 ";
      }
  else{
      $found=1;
      $query="WHERE op3=$op3 ";
      }
  }
  </code></pre>
  
  <p>Now, obviously, this is quite annoying to implement. Is there any easier method? </p>
  
  <p>Thanks.</p>