Search Query In PHP/MySQLi

June 4, 2020
PHP
search query in mysqli

In this tutorial, we will create a Search Query In MySQLi using PHP. This code will search specific data in the MySQLi server when the user submits the keyword. The code use MySQLi WHERE LIKE query to begin a search query to display a list of data 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 manages a database server to handle a bulk of data per transaction. It describes as an advance technology that manages 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 locates the database file inside the folder of the application then click ok.

search query in php

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.

<?php
    $conn = mysqli_connect("localhost", "root", "", "db_search_query");
 
    if(!$conn){
        die("Error: Failed to connect to database!");
    }
?>

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.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
    </head>
<body>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="https://sourceodester.com">Sourcecodester</a>
        </div>
    </nav>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">PHP - Search Query In MySQLi</h3>
        <hr style="border-top:1px dotted #ccc;"/>
        <div class="col-md-4">
            <form method="POST" action="add.php">
                <div class="form-group">
                    <label>Book Title</label>
                    <input type="text" name="book" class="form-control" required="required"/>
                </div>	
                <div class="form-group">
                    <label>Author Name</label>
                    <input type="text" name="author" class="form-control"required="required"/>
                </div>
                <div class="form-group">
                    <label>Date Published</label>
                    <input type="date" name="date_published" class="form-control"required="required"/>
                </div>
                <center><button class="btn btn-primary" name="add">Add</button></center>
            </form>
        </div>
        <div class="col-md-8">
            <form method="POST" action="">
                <div class="form-inline">
                    <input type="text" class="form-control" name="keyword" placeholder="Search here..." required="required"/>
                    <button class="btn btn-success" name="search">Search</button>
                </div>
            </form>
            <br />
        <?php include 'search_query.php'?>		
        </div>
</body>	
</html>

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.

<?php
    require 'conn.php';
    if(ISSET($_POST['search'])){
?>
    <table class="table table-striped">
        <thead class="alert-info">
            <tr>
                <th>Book</th>
                <th>Author</th>
                <th>Date Published</th>
            </tr>
        </thead>
        <tbody>
            <?php
                $keyword = $_POST['keyword'];
                $query = mysqli_query($conn, "SELECT * FROM `book` WHERE `title` LIKE '%$keyword%' or `author` LIKE '%$keyword%'") or die(mysqli_error());
                $count = mysqli_num_rows($query);
                if($count > 0){
                    while($fetch = mysqli_fetch_array($query)){
            ?>
                    <tr>
                        <td><?php echo $fetch['title']?></td>
                        <td><?php echo $fetch['author']?></td>
                        <td><?php echo $fetch['date_published']?></td>
                    </tr>
            <?php
                    }
                }else{
                    echo "<tr><td colspan='3' class='text-danger'><center>No result found!</center></td></tr>";
                }
            ?>
        </tbody>
    </table>
<?php		
    }else{
?>
    <table class="table table-striped">
        <thead class="alert-info">
            <tr>
                <th>Book</th>
                <th>Author</th>
                <th>Date Published</th>
            </tr>
        </thead>
        <tbody>
            <?php
                $query = mysqli_query($conn, "SELECT * FROM `book` ORDER BY `title` ASC") or die(mysqli_error());
                while($fetch = mysqli_fetch_array($query)){
            ?>
            <tr>
                <td><?php echo $fetch['title']?></td>
                <td><?php echo $fetch['author']?></td>
                <td><?php echo $fetch['date_published']?></td>
            </tr>
            <?php
                }
            ?>
        </tbody>
    </table>
<?php
    }
?>

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!

Facebook Comments

Leave a Reply

Your email address will not be published.