title: PHP PDO MySQL query LIKE -> multiple keywords
body: <p>I have a users table in MySQL and would like a search by name. Right now I have the following code:</p>
  
  <pre><code>&lt;?php
  $search = @$_GET['q'];
  $search = strtoupper($search);
  $search = strip_tags($search);
  $search = trim($search);
  
  $query = $con-&gt;prepare('SELECT * FROM `users` WHERE name LIKE ?');
  $query-&gt;execute(array('%'.$search.'%'));
  
  $result = $query-&gt;rowCount();
  echo $result;
  ?&gt;
  </code></pre>
  
  <p>The problem is that I want to have multiple keywords. Let's say someone types "Here should be a nice name of a person" then it would search for "here", "should", "be" etc. and display results for every row where there words are in 'name' column. I searched on the web and read that it is possible to do "OR name LIKE ?" as many times as the keywords, but I could not really get it working and I'm not sure if it is optimized enough with ~20 words (in case they search with that many words). If it should be used, can you help me change my code so it would search for every word independently?</p>
  
  <p>Thank you!</p>
  
  <p>EDIT:</p>
  
  <p>I was able to fix this issue by one guy who posted in this thread. The following solution works for me:</p>
  
  <pre><code>&lt;?php
  $search = isset($_POST['q']) ? $_POST['q'] : '';
  $search = strtoupper($search);
  $search = strip_tags($search);
  $search = trim($search);
  
  $split_words = explode(" ", $search);
  
  if(count($split_words) &gt; 0) {
  
      $sql = "SELECT * FROM `users` WHERE ";
  
      for($i=0 ; $i &lt; count($split_words); $i++){
          $sql .= " name LIKE ? OR";
      }
  
      $sql = substr($sql , 0, -3); //Remove last 3 characters OR with space
      array_walk($split_words, "addPercentage");
  
  
      $query = $con-&gt;prepare($sql);
      $query-&gt;execute($split_words);
  }
  
  function addPercentage(&amp;$value, $key) {
      $value = '%'.$value.'%';
  }
  ?&gt;
  </code></pre>