title: PHP Search Using Multiple Words
body: <p>I am trying to create a search function where a user can input two words into a text field and it will split the words and construct a MySQL query.</p>
  
  <p>This is what I have so far.</p>
  
  <pre><code>$search     = mysql_real_escape_string( $_POST['text_field']);
  $search     = explode(" ", $search);
  
  foreach($search as $word)
  {
      $where = "";
      $where      .= "product_code LIKE '%". $word ."%'";
      $where      .= "OR description LIKE '%". $word ."%'";
  
      $query      = "SELECT * FROM customers WHERE $where";
      $result     = mysql_query($query) or die();
  
      if(mysql_num_rows($result))
      {
          while($row = mysql_fetch_assoc($result))
          {
              $customer['value']  = $row['id'];
              $customer['label']  = "{$row['id']}, {$row['name']} {$row['age']}";
              $matches[]          = $customer;
          }
      }
      else
      {
          $customer['value']  = "";
          $customer['label']  = "No matches found.";
          $matches[]          = $customer;
      }
  }
  
  $matches = array_slice($matches, 0, 5); //return only 5 results
  </code></pre>
  
  <p>It constructs and runs the query, but returns funny results.</p>
  
  <p>Any help would be appreciated.</p>