PHP - Search Query In MySQLi

Submitted by: 
Language: 
Visitors have accessed this post 211 times.

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 https://www.apachefriends.org/index.html.

And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

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.
tut1

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");
  3.  
  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="<a href="https://sourceodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourceodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Search Query In MySQLi</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-4">
  19. <form method="POST" action="add.php">
  20. <div class="form-group">
  21. <label>Book Title</label>
  22. <input type="text" name="book" class="form-control" required="required"/>
  23. </div>
  24. <div class="form-group">
  25. <label>Author Name</label>
  26. <input type="text" name="author" class="form-control"required="required"/>
  27. </div>
  28. <div class="form-group">
  29. <label>Date Published</label>
  30. <input type="date" name="date_published" class="form-control"required="required"/>
  31. </div>
  32. <center><button class="btn btn-primary" name="add">Add</button></center>
  33. </form>
  34. </div>
  35. <div class="col-md-8">
  36. <form method="POST" action="">
  37. <div class="form-inline">
  38. <input type="text" class="form-control" name="keyword" placeholder="Search here..." required="required"/>
  39. <button class="btn btn-success" name="search">Search</button>
  40. </div>
  41. </form>
  42. <br />
  43. <?php include 'search_query.php'?>
  44. </div>
  45. </body>
  46. </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';
  3.  
  4. if(ISSET($_POST['add'])){
  5. $book = $_POST['book'];
  6. $author = $_POST['author'];
  7. $date_published = $_POST['date_published'];
  8.  
  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!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.