Skip to main content

PHP - Search Query In MySQLi

In this tutorial we will create a Search Query In MySQLi using PHP. This code will search a specific data in the MySQLi server when the user submit the keyword. The code use MySQLi WHERE LIKE query to begin a search query in order to display a list of data that based on a given keyword. This is a user-friendly kind of program feel free to modify it and use it as your own.

We will be using PHP as a scripting language that manage a database server to handle a bulk of data per transaction. It describe as an advance technology that manage both server and control-block of your machine.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server

And, this is the link for the bootstrap that i used for the layout design

Creating Database

Open your database web server then create a database name in it db_search_query, after that click Import then locate the database file inside the folder of the application then click ok.

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

  1. <?php
  2. $conn = mysqli_connect("localhost", "root", "", "db_search_query");
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Search Query In MySQLi</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-4">
  18. <form method="POST" action="add.php">
  19. <div class="form-group">
  20. <label>Book Title</label>
  21. <input type="text" name="book" class="form-control" required="required"/>
  22. </div>
  23. <div class="form-group">
  24. <label>Author Name</label>
  25. <input type="text" name="author" class="form-control"required="required"/>
  26. </div>
  27. <div class="form-group">
  28. <label>Date Published</label>
  29. <input type="date" name="date_published" class="form-control"required="required"/>
  30. </div>
  31. <center><button class="btn btn-primary" name="add">Add</button></center>
  32. </form>
  33. </div>
  34. <div class="col-md-8">
  35. <form method="POST" action="">
  36. <div class="form-inline">
  37. <input type="text" class="form-control" name="keyword" placeholder="Search here..." required="required"/>
  38. <button class="btn btn-success" name="search">Search</button>
  39. </div>
  40. </form>
  41. <br />
  42. <?php include 'search_query.php'?>
  43. </div>
  44. </body>
  45. </html>

Creating the PHP Query

This code contains the php query of the application. This code will store the form inputs to the MySQLi database server. To make this just copy and write these block of codes below inside the text editor, then save it as add.php.

  1. <?php
  2. require_once 'conn.php';
  4. if(ISSET($_POST['add'])){
  5. $book = $_POST['book'];
  6. $author = $_POST['author'];
  7. $date_published = $_POST['date_published'];
  9. mysqli_query($conn, "INSERT INTO `book` VALUES('', '$book', '$author', '$date_published')") or die(mysqli_error());
  10. echo "<script>alert('Add a new Book!')</script>";
  11. echo "<script>window.location = 'index.php'</script>";
  12. }
  13. ?>

Creating the Main Function

This code contains the main function of the application. This code will search a specific data in the database when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as search_query.php.

  1. <?php
  2. require 'conn.php';
  3. if(ISSET($_POST['search'])){
  4. ?>
  5. <table class="table table-striped">
  6. <thead class="alert-info">
  7. <tr>
  8. <th>Book</th>
  9. <th>Author</th>
  10. <th>Date Published</th>
  11. </tr>
  12. </thead>
  13. <tbody>
  14. <?php
  15. $keyword = $_POST['keyword'];
  16. $query = mysqli_query($conn, "SELECT * FROM `book` WHERE `title` LIKE '%$keyword%' or `author` LIKE '%$keyword%'") or die(mysqli_error());
  17. $count = mysqli_num_rows($query);
  18. if($count > 0){
  19. while($fetch = mysqli_fetch_array($query)){
  20. ?>
  21. <tr>
  22. <td><?php echo $fetch['title']?></td>
  23. <td><?php echo $fetch['author']?></td>
  24. <td><?php echo $fetch['date_published']?></td>
  25. </tr>
  26. <?php
  27. }
  28. }else{
  29. echo "<tr><td colspan='3' class='text-danger'><center>No result found!</center></td></tr>";
  30. }
  31. ?>
  32. </tbody>
  33. </table>
  34. <?php
  35. }else{
  36. ?>
  37. <table class="table table-striped">
  38. <thead class="alert-info">
  39. <tr>
  40. <th>Book</th>
  41. <th>Author</th>
  42. <th>Date Published</th>
  43. </tr>
  44. </thead>
  45. <tbody>
  46. <?php
  47. $query = mysqli_query($conn, "SELECT * FROM `book` ORDER BY `title` ASC") or die(mysqli_error());
  48. while($fetch = mysqli_fetch_array($query)){
  49. ?>
  50. <tr>
  51. <td><?php echo $fetch['title']?></td>
  52. <td><?php echo $fetch['author']?></td>
  53. <td><?php echo $fetch['date_published']?></td>
  54. </tr>
  55. <?php
  56. }
  57. ?>
  58. </tbody>
  59. </table>
  60. <?php
  61. }
  62. ?>

There you have it we successfully created Search Query In MySQLi using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.