Simple Search Option Using PHP, MySQL

Search functionality is a basic need for any kind of website. You almost find it on every website. Here is the traditional way of working with search system using PHP and MySQL.

Which includes majorly three steps.

1. Database connection
2. Form to search
3. Fetching related data from database table.

Before you go to these you need to setup local server and database.
If you don't know how to do, you can check out here Setting up the server.

Once you set up the server run following queries.

To create a table.

  `UserID` int(12) NOT NULL,
  `FirstName` varchar(48) NOT NULL,
  `LastName` varchar(48) NOT NULL,
  `Email` varchar(128) NOT NULL,
  `Password` varchar(20) NOT NULL,
  `City` varchar(48) NOT NULL


Setting up the primary key.


Insert some sample data in to table.

INSERT INTO `user` (`UserID`, `FirstName`, `LastName`, `Email`, `Password`, `City`) VALUES
(7, 'Rahul', 'Rajshekaran', '', 'Rahul@123', 'Pune'),
(8, 'Mahesh', 'Krishna', '', 'Mahesh@123', 'Delhi'),
(9, 'Mahidar', 'kumar', '', 'Mahidar@123', 'Pune'),
(10, 'Mahpal', 'yadhav', '', 'Mahipa@123', 'Delhi');

        <link rel="stylesheet" href="">
                position: absolute;
                width: 300px;
                height: 350px;
                z-index: 15;
                top: 40%;
                left: 50%;
                margin: -100px 0 0 -150px;
                border: 1px solid #8080804a;
                border-radius: 5px;
                background: #8080804a;
                width: 299px;
                background: #c30000;
                text-align: center;
                font-size: 20px;
                margin-bottom: 20px;
                padding: 5px;
                padding: 10px;
                margin-top: 5px;
                margin-top: 20px;
                background-color: #c30000;
                border-color: #c30000;
                background-color: #c30000;
                border-color: #c30000;
                width: 299px;
                background: #5d5d5d;
                text-align: center;
                font-size: 20px;
                margin-top: 180px;
                padding: 5px;
        <div class="login-box">

            <form method="GET">
                <div class="form-header">SEARCH</div>
                <div class="col-md-12">  
                    <input type="text" name="query" class="form-control"/>
                <div class="col-md-12">  
                    <input type="submit" value="Search" name="search" class="btn btn-primary"/>

            //Connecting to database
            $DatabaseServer = "localhost";
            $DatabaseUsername = "root";
            $DatabasePassword = "root";
            $DatabaseName = "demo";

            $Connection = mysqli_connect($DatabaseServer, $DatabaseUsername, $DatabasePassword, $DatabaseName);

            if ($Connection === false) {
                die("ERROR: Could not connect. " . mysqli_connect_error());

            // If query is set get value sent over search form, else define it to null
            if (isset($_GET['query'])) {
                $query = $_GET['query'];
            } else {
                $query = '';

            // You can set minimum length of the query as you wish
            $min_length = 3;
            if (isset($_GET['search'])) {

                echo "<div class='result'>";
                // If query length is more or equal to minimum length then
                if (strlen($query) >= $min_length) {
                    $query = htmlspecialchars($query);

                    $query = mysqli_real_escape_string($Connection, $query);
                    $sql = "SELECT * FROM user WHERE (`FirstName` LIKE '" . $query . "%')";

                    $raw_results = mysqli_query($Connection, $sql);

                    // If one or more rows are returned
                    if (mysqli_num_rows($raw_results) > 0) {
                        while ($results = mysqli_fetch_array($raw_results)) {
                            echo "<h4>" . $results['FirstName'] . "</h4>";
                    } else { //If no records found
                        echo "No results found";
                } else { // If query length is less than minimum
                    echo "Minimum length is " . $min_length;
                echo "</div>";

No comments:

Post a Comment


php (35) javascript (31) phpjavascript (30) jquery (23) html (20) mysql (14) database (9) codeigniter (4) json (4) bar chart (2) calendar (2) column chart (2) framework (2) google maps (2) query (2) tables (2) url (2) dropdown (1)

Popular Posts