title: Using PDO to make a MySQL search query and display list of results
body: <p>As the title suggests, I am trying to display a list of search results using PDO and MySQL...I have a table of recipes, which have recipe_id, name &amp; description. I would like to have a search box which can find a key word IN the name or description, i.e. "salad" or "carrots", and return a list of all matching recipes by displaying only their names. Before I switched to using PDO, I had the following code which did exactly what I needed:</p>
  
  <pre><code>&lt;?php
  include ("dbconnect.php");
  
  if (!isset($_POST['search']))    {
      header("Location:index.php");
  }
  
  $search_sql="SELECT * FROM Recipe WHERE name LIKE '%".$_POST['search']."%' OR description LIKE '%".$_POST['search']."%'";
  $search_query=mysql_query($search_sql);
  if (mysql_num_rows($search_query)!=0) {
      $search_rs=mysql_fetch_assoc($search_query); }
  ?&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
      &lt;title&gt;Untitled Document&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;h3&gt;Search results&lt;/h3&gt;
  &lt;?php
  if (mysql_num_rows($search_query)!=0) {
      do { ?&gt;
          &lt;p&gt;&lt;?php echo $search_rs['name']; ?&gt;&lt;/p&gt;
      &lt;?php }
      while ($search_rs=mysql_fetch_assoc($search_query));
  }
  else {
      echo "No results found";
  }
  ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>However, I am having difficulties doing the same with PDO...I have come up with the following code so far, but I suspect I am doing it wrong and furthermore I do not know how to display the actual results...I would be very grateful if anyone can provide some assistance, and please excuse my insufficient knowledge on the matter, I am still a newbie.</p>
  
  <pre><code>        &lt;?php
  include ("dbconnect.php");
  
  if (!isset($_POST['search']))    {
      header("Location:index.php");
  }
  
  // keep track post values
  $name = "%".$_POST['search']."%";
  $description = "%".$_POST['search']."%";
  
  $pdo = Database::connect();
  $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $sql ='SELECT * FROM recipe WHERE name LIKE ? OR description LIKE ?';
  $q = $pdo-&gt;prepare($sql);
  $q-&gt;execute(array($name,$description));
  $data = $q-&gt;fetchAll(PDO::FETCH_ASSOC);
  Database::disconnect();
  ?&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;link   href="css/bootstrap.min.css" rel="stylesheet"&gt;
      &lt;script src="js/bootstrap.min.js"&gt;&lt;/script&gt;
      &lt;title&gt;Untitled Document&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div class="container"&gt;
      &lt;div class="row"&gt;
          &lt;table class="table table-striped table-bordered"&gt;
              &lt;thead&gt;
                   &lt;tr&gt;
                      &lt;th&gt;Search Results&lt;/th&gt;
                   &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
      &lt;?php
      if ($data != null) {
          foreach($data as $row)
               {
                   echo '&lt;tr&gt;';
                   echo '&lt;td&gt;'. $row['name'] . '&lt;/td&gt;';
                   echo '&lt;/tr&gt;';
               }
          }else
               {
                   echo '&lt;td&gt;'. "No results found" .'&lt;/td&gt;';
               }?&gt;
              &lt;/tbody&gt;
          &lt;/table&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>