title: Speed up query for Autocomplete inputbox
body: <p>I'm trying to implement an AutoComplete for my search box.</p>
  
  <p>I have a database with a table of books, so when a user starts typing in the search box, i want to be able to show them a suggestion box with the first 6 titles that match the words it's being typed.</p>
  
  <p>I used Ajax to query a PHP that have the following code:</p>
  
  <pre><code>    $terms           = explode( " ", $data-&gt;word );
      $terminos        = '';
      foreach( $terms as $term ) {
          if ( $term == '' || $term == ' ' || strlen($term)==0 ) continue;
          if ( $terminos != '' ) $terminos .= " AND title LIKE '%$term%' ";
          else $terminos .= " title LIKE '%$term%' ";
      }
      $pdo = MySQLUtil::getConnection();
      $query = "SELECT title
                  FROM books
                  WHERE :terminos
                  LIMIT 6";
  
      $stmt = $pdo-&gt;prepare($query);
      $stmt-&gt;bindParam(':terminos',   $terminos,      PDO::PARAM_STR);
      $stmt-&gt;execute();
      $result = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC);
      if ($result) {
          return $result;
      } else {
          return false;
      }
  </code></pre>
  
  <p>This solution is very slow and need to know how could I speed up the query.</p>
  
  <p>Any ideas?</p>
  
  <p>Thanks in advance.</p>