title: How to create a search using PHP, mysqli and a html form
body: <p>I want to create a form which allows the user to type in a search and have it pick up the right values from a database and display them, for some reason I can't get my query to work it just displays "could not search"</p>
  
  <p>Here is my php code</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code> &lt;?php
  
    include "connect.php";
  
    $output = '';
  
    if(isset($_POST['search'])) {
      $search = $_POST['search'];
      $search = preg_replace("#[^0-9a-z]i#","", $search);
  
      $query = mysqli_query("SELECT * FROM house WHERE town LIKE '%$search%'") or die ("Could not search");
      $count = mysqli_num_rows($query);
      
      if($count == 0){
        $output = "There was no search results!";
  
      }else{
  
        while ($row = mysqli_fetch_array($query)) {
  
          $town = $row ['town'];
          $street = $row ['street'];
          $bedrooms = $row ['bedrooms'];
          $bathroom = $row ['bathrooms'];
  
          $output .='&lt;div&gt; '.$town.''.$street.''.$bedrooms.''.$bathrooms.'&lt;/div&gt;';
  
        }
  
      }
    }
  
    ?&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Here is my form</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code> &lt;form action ="home.php" method = "post"&gt;
    
            &lt;input name="search" type="text" size="30" placeholder="Belfast"/&gt;
  
            &lt;input type="submit" value="Search"/&gt;
  
            &lt;/form&gt; 
  
            &lt;?php print ("$output");?&gt;</code></pre>
  </div>
  </div>
  </p>