How to Create Search Engine Using PHP and MySql

How to Create Search Engine Using PHP and MySql

Today I will show you how to create search engine  using  php and mysql.  
Search engine has become very useful feature in php, using this we can get the information on our choice.

So in this post, I have already a table with some data and I want to search data by name for example, I have one table, name is student and this table contains some information shown below


Create Search Engine Using PHP and MySql

In this post need following files
  •           config.php
  •           search_stu.php

Follow these Steps

Step 1.   First Create config.php File

     <?php

        $servername    = "localhost";
        $username       = "root";
        $password        = "";
        $dbname          = "tutorials"; 

        $conn = new mysqli($servername, $username, $password, $dbname);

        if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
        } 

     ?>

Step 2.   Second Create search_stu.php File

     <?php



        include_once('config.php');

        $output = '';

        if(isset($_POST['search']))

        {
          $search_query = $_POST['search'];
          $search_query = preg_replace("#[^0-9a-z]#i","",$search_query);

          $query = "select * from students where stu_name like '%$search_query%'";
          $result = mysqli_query($conn, $query);
          $count = mysqli_num_rows($result);
          if($count == 0)
          {
            $output = 'No Search Results';
          }
         else
          {
            while($row = mysqli_fetch_array($result))
          {
            $stu_name    = $row['stu_name'];
        
            $output .= '<div>'.$stu_name.'</div>';
          }
        }
      }

    ?>
     
     <!DOCTYPE html>
     <html>
     <head>
       <title>Search</title>
     </head>
     <body>
       <form method="post" action="search_stu.php">
         <input type="text" name="search" placeholder="Search by Name">
         <input type="submit" value="Go" /> 
       </form>
       <div style="text-align:left; margin-top:10px;">
         <?php echo("$output"); ?>
       </div>
     </body>
     </html>

Step 3.   Now Go to browser and type localhost/tutorials/search_stu.php screen shown below


Create Search Engine Using PHP and MySql

After that put some value like 'a' and press Go button, screen shown below


Create Search Engine Using PHP and MySql
If you want to search like neha just put the name neha and press Go button, screen shown below


Create Search Engine Using PHP and MySql

If value does not exit in database, for example i put value like z and press Go button message shown below


Create Search Engine Using PHP and MySql



For More Information also see









                  I hope, it helped you to understand, How to Create Search Engine Using PHP and MySql......Thanks

About MSK TUTORIAL

Hi...I am Md Sahjad Karim and I am Engineering Graduate (B.E in Computer Science and Engineering) working as a Web Developer and write Blog Articles. http://www.msktutorial.com/ was launched on july 2016 to provide free online hints, tips, guides and tutorials related to technology(PHP, CodeIgniter, WordPress, Blogger etc). My primary goal is to provide the good, quality and easily to understand the articles to readers around the world.
    Blogger Comment
    Facebook Comment

4 comments:

Nurulhuda said...

Need script like or similar YouTube, if any please link

MSK TUTORIAL said...

hi...Md Nuraul, i will provide YouTube link this week...thanksss

Unknown said...

Good information
href=”https://indglobal.in/”>website design in bangalore
href=”https://indglobal.in/”>ecommerce website design in bangalore
href=”https://indglobal.in/”>mobile app development

Unknown said...

This “php ustad” tutorial blog is designed for beginners – to learn php at home step by step using simple programming examples

Post a Comment

Thanks For Visiting Blog...