How to Create Search Query using PHP/MySQL

In this tutorial, we are going to create Search Query using PHP/MySQL. Search query using PHP/MySQL is a simple PHP source code that the users enable to search in the table. Creating one TextBox for the user where they enter the desired word to search and the table with our data in a data table as you can see in the image below. ResultHere's the source code of the image above. Kindly copy and paste to your BODY tag of your page.
  1. <div class="row-fluid">
  2. <div class="span6">
  3. <div class="span6">
  4. <form method="POST" action="search.php" class="navbar-search pull-left">
  5. <input type="text" name="search" class="search-query" placeholder="Search">
  6. </form>
  7. </div>
  8. <br />
  9. <br />
  10. <br />
  11. <table class="table table-bordered table-hover table-striped">
  12. <tr>
  13. <th>Title</th>
  14. <th>Author</th>
  15. <th>Category</th>
  16. </tr>
  17. </thead>
  18. <?php
  19. $query = mysql_query("select * from book") or die(mysql_error());
  20.  
  21. while ($row = mysql_fetch_array($query))
  22. {
  23. ?>
  24.  
  25. <tr>
  26. <td><?php
  27. echo $row['title']; ?></td>
  28. <td><?php
  29. echo $row['author']; ?></td>
  30. <td><?php
  31. echo $row['category']; ?></td>
  32.  
  33. </tr>
  34. <?php
  35. }
  36.  
  37. ?>
  38. </tbody>
  39. </table>
  40. </div>
  41. </div>
We are going to search the "Programming" word. Let's enter to the TextBox and it will show in the image below what happen next. ResultAs you can see in the image above, only has a Programming word display on our table. Here's the source code for the searching data in our data table.
  1. <div class="span12">
  2. <br />
  3. <a href="index.php" class="btn btn-success">Back</a>
  4. <br />
  5. <br />
  6. <div class="row-fluid">
  7. <div class="span6">
  8. <div class="span6">
  9. <form method="POST" class="navbar-search pull-left">
  10. <input type="text" name="search" class="search-query" placeholder="Search">
  11. </form>
  12. </div>
  13. <br />
  14. <br />
  15. <br />
  16. <table class="table table-bordered table-hover table-striped">
  17. <tr>
  18. <th>Title</th>
  19. <th>Author</th>
  20. <th>Category</th>
  21. </tr>
  22. </thead>
  23. <?php
  24.  
  25. if (isset($_POST['search']))
  26. {
  27. $search = $_POST['search'];
  28. $query = mysql_query("select * from book where category like '%$search%' or title like '%$search%' or author like '%$search%'") or die(mysql_error());
  29. while ($row = mysql_fetch_array($query))
  30. {
  31. ?>
  32.  
  33. <tr>
  34. <td><?php
  35. echo $row['title']; ?></td>
  36. <td><?php
  37. echo $row['author']; ?></td>
  38. <td><?php
  39. echo $row['category']; ?></td>
  40.  
  41. </tr>
  42. <?php
  43. }
  44. }
  45.  
  46. ?>
  47.  
  48.  
  49.  
  50. </tbody>
  51. </table>
  52. </div>
  53. </div>
  54. </div>
Hope that this tutorial will help you a lot. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment