title: filter data pdo
body: <p>What I am trying to do is similar to this. <a href="https://stackoverflow.com/questions/13206530/search-filtering-with-php-mysql">Search Filtering with PHP/MySQL</a></p>
  
  <pre><code>  &lt;?php 
          require 'con.php';
  
          $minage     = $_POST['data'][0];
          $maxage     = $_POST['data'][1];
          $gender     = $_POST['data'][2];
          $religion   = $_POST['data'][3];
  
          $query = "SELECT CONCAT(firstname, ' ', middlename, ' ', lastname, ' ', extension_name) as fullname, TIMESTAMPDIFF(YEAR, birthday ,NOW()) as age FROM mytable";
  $filter = array();
  
  
         if($gender != -1){
             $gender = substr($gender, 1, -1);
             $filter[] = "gender = :gender";
         }
         if($religion != -1){
              $filter[] = "religion = :religion";
         }
  
         if(count($filter) &gt; 0){
             $query .= " WHERE " . implode(' AND ', $filter);
             $sql = $connection-&gt;prepare($query);
       -&gt;    $sql-&gt;bindParam(':gender', $gender, PDO::PARAM_STR);
       -&gt;    $sql-&gt;bindParam(':religion', $religion, PDO::PARAM_STR);
             $sql-&gt;execute();
             $res = $sql-&gt;fetchAll();
        }else{
             $sql = $connection-&gt;prepare($query);
             $sql-&gt;execute();
             $res = $sql-&gt;fetchAll();
        }
    ?&gt;
  
     &lt;?php foreach($res as $row): ?&gt;
           &lt;div&gt;&lt;?php echo $row['fullname'];?&gt;&lt;/div&gt;
     &lt;?php endforeach; ?&gt;
  </code></pre>
  
  <p>When I select a gender and religion on my dropdown, the result is fine.</p>
  
  <p>But when I select only one, let's say gender, I received an error:</p>
  
  <blockquote>
    <p>number of bound variables does not match number of tokens</p>
  </blockquote>
  
  <p>I'm a bit confused where to place <code>$sql-&gt;bindParam(...);</code>. I guess this is the cause of my error? Or if there's more 'error' or if there's anything that's not right, please correct me. Thank you in advance. </p>