title: Using the LIKE operator with % for a keyword search with PDO prepared statements
body: <p>I am trying to write a keyword search using PDO prepared statements.  Ideally, I'd like to make use of the LIKE operator to search for a substring inside the field value.  Here is my code:</p>
  
  <pre><code>$statement = $this-&gt;db-&gt;prepare("select * from whatever where title like ? or author like ?");
  $statement-&gt;execute(array("%$titleKeyword%","%$authorKeyword%"));
  $rows = $statement-&gt;fetchAll(PDO::FETCH_ASSOC);
  </code></pre>
  
  <p>Unfortunately, $rows is always empty when I try this.  However, if I copy the SQL into phpMyAdmin, and substitute '%keyword%' for each of the ? symbols, it works fine (I get results when the keyword used exists).</p>
  
  <p>I have also tried the following code:</p>
  
  <pre><code>$statement = $this-&gt;db-&gt;prepare("select * from whatever where title like :titleKeyword or author like :authorKeyword");
  $statement-&gt;bindValue(":titleKeyword",  '%'.$titleKeyword.'%',  PDO::PARAM_STR);
  $statement-&gt;bindValue(":authorKeyword", '%'.$authorKeyword.'%', PDO::PARAM_STR);
  $statement-&gt;execute();
  $rows = $statement-&gt;fetchAll(PDO::FETCH_ASSOC);
  </code></pre>
  
  <p>I had read in another question that you are supposed to include the % when binding the parameters, not in the SQL itself (pre-prepared statement), but that doesn't work.</p>
  
  <p>I could resort to just inserting the keyword directly into the SQL (after doing some sanitization), but I want to stick with prepared statements.  Any help would be greatly appreciated.</p>