Labels

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)

Wednesday, March 7, 2018

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.

CREATE TABLE `user` (
  `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

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Setting up the primary key.

ALTER TABLE `user`
  ADD PRIMARY KEY (`UserID`);

Insert some sample data in to table.

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






<html>
    <head>
        <title>Search</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <style>
            .login-box{
                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;
            }
            .form-header{
                width: 299px;
                height:40px;
                background: #c30000;
                color:white;
                text-align: center;
                font-size: 20px;
                margin-bottom: 20px;
                padding: 5px;
            }
            .form-control{
                padding: 10px;
                margin-top: 5px;
            }
            .btn-primary{
                margin-top: 20px;
                background-color: #c30000;
                border-color: #c30000;
            }
            .btn-primary:hover{
                background-color: #c30000;
                border-color: #c30000;
            }
            .result{
                width: 299px;
                height:100px;
                background: #5d5d5d;
                color:white;
                text-align: center;
                font-size: 20px;
                border-radius:5px;
                margin-top: 180px;
                padding: 5px;
            }
        </style>
    </head>
    <body>
        <div class="login-box">

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


            <?php
            //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>";
            }
            ?>
        </div>
    </body>
</html>







More Posts

FOLLOW