title: Using PDO query, without prepared statements, with multiple LIKE statements from multiple HTML input fields
body: <p>I have successfully gotten queries to execute and print in PDO, but I'm doing something wrong here. The important part of the code for this question is in the last couple blocks of code; I'm including the first portion just for clarity. </p>
  
  <p>This code connects to an HTML form with multiple input fields. The PHP constructs a query by appending the data from each field with ANDs in the WHERE statement. </p>
  
  <p>This is what throws me: I echo the $query variable, and I can see that the query is formed properly, but when I then try to print the query results, no results are printed.</p>
  
  <p>I wrestled with using prepared statements here, and decided to try getting the code to work first without them after failing to construct a prepared statement with varying numbers of parameters. I did try, with the help of this post: <a href="https://stackoverflow.com/questions/13214712/like-query-using-multiple-keywords-from-search-field-using-pdo-prepared-statemen">LIKE query using multiple keywords from search field using PDO prepared statement</a></p>
  
  <p>So, setting aside prepared statements for the moment, can anyone tell me what I'm doing wrong here? Any help would be greatly appreciated.</p>
  
  <pre><code>&lt;?php
  if(isset($_POST['submit'])) {
  
  // define the list of fields
  $fields = array('titleSearch', 'keywordSearch', 'fullSearch', 'fromYear', 'toYear', 
      'fromSeconds', 'toSeconds', 'withSound', 'withColor');
  $conditions = array();
  
  // loop through the defined fields
  foreach($fields as $field){
      // if the field is set and not empty
      if(isset($_POST[$field]) &amp;&amp; $_POST[$field] != '') {
          // create a new condition, using a prepared statement
          $conditions[] = "$field LIKE CONCAT ('%', $_POST[$field], '%')";
      }
  }
  
  // build the query
  $query = "SELECT keyframeurl, videoid, title, creationyear, sound, color, 
       duration, genre FROM openvideo ";
  
  // if there are conditions defined, append them to the query
  if(count($conditions) &gt; 0) {
      $query .= "WHERE " . implode(' AND ', $conditions);
  }
  
  //confirm that query formed correctly
  echo $query; 
  
  //print query results
  foreach ($dbh-&gt;query($query) as $row){
     print $row['videoid'].' - '.$row['title'].'&lt;br /&gt;';
  }
  }
  ?&gt;
  </code></pre>