03 June, 2014

How to Make a Simple Search Engine using PHP and MySQLi

This post i make it small search engine using php and mysqli. this search engine search to user date in database.similar like google search engine. its should be find keywords in database record to search related description similar character. 

search-engine-img

Live Demo         Download



Update post : http://www.mostlikers.com/2016/05/create-your-own-search-engine-using-php.html

Database

Database connect to mysqli query  create below format table. the fields to store on user given information.


CREATE TABLE `search`
(
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(50) NOT NULL,
`desp` varchar(50) NOT NULL,
)

Index.php

<?php
$db = new mysqli('localhost', 'root', '', 'test');
session_start();
    if(isset($_POST['submit']))
    {
        $search=$_POST['search'];
        $_SESSION['title']= $search;
        if(($_SESSION['title'])!="")
        {
        header("location:index.php");
        }
        else
        {
        echo "<script> alert('search box as been empty') </script>";
        }
    }
?>
   
    <div class="login">
    <form method="post">
    <p align="center"><img src="logos.png" /></p>
    <p><?php if(isset($_SESSION['title'])) { ?>
    <input name="search" type="search" list="searchkey" value="<?php echo $_SESSION['title'];?>" 
   class="search" />
    <?php } else { ?>
    <input name="search" type="search" list="searchkey" placeholder=""  class="search" />
    <?php } ?></p>
    <datalist id="searchkey">
    <?php
    $tile=$db->query("SELECT * FROM `search`");
    while($storetitle=mysqli_fetch_object($tile))
    {
    ?>
    <option  value="<?php echo $storetitle->title ?>">
    <?php } ?>
    </datalist>
    <p align="center">
    <input type="submit" name="submit" id="click" class="but" value="Mostliker search"  />
    <input type="submit" name="submit" class="but" value="I m Feeling lucky" /> </p> 
    <p class="lang">Add content: 
    <a href="add-view.php">Hindi</a>&nbsp;<a href="add-view.php"> Bengali </a>&nbsp;    
    Kannada</a>&nbsp;<a href="add-view.php"> Malayalam</a></p>

    <?php if(isset($_SESSION['title'])) {
    if(($_SESSION['title']!=""))
    {
    $data=$_SESSION['title'];
    $view=$db->query("select * from search where title like '%$data%' 
   || desp like '%$data%' limit 10 ");
    $check=mysqli_num_rows($view);
    if($check!="")
    {
    while($descri=mysqli_fetch_object($view))
    {
    ?>
    <div class="reslt">
    <h3 id="resuil-title"><?php echo $descri->title; ?></h3>
    <p class="Description">
    <?php $description = str_replace($data, '<span class="highlight">'.$data."</span>", 
    $descri->desp);
    echo $description; ?><p>
    <hr>
    </div>
    <?php } } else { ?>
    <div class="reslt">
    <h3 id="resuil-title">Searching Data not fond</h3>
    <p class="Description">
    Add new data enter and check the correct keyword
    <p><hr>
    </div>
    <?php } } } ?>
    </form>
    </div>
    </body>
    </html>

add-view.php

<?php 
$db = new mysqli('localhost', 'root', '', 'test');// change the database connections
?>
<?php
if(isset($_POST['ADD'])!="")
 {   
    $title=$_POST['title'];
    $description=$_POST['desc'];
    $insert=$db->query("INSERT INTO `search`(title,desp) values('$title','$description')"); 
    if($insert)
    {
        echo "<script> alert('search the title now') </script>";
    }
    else
    { 
         echo "<script> alert('unable insert to your details') </script>";  
    } 
 }
?>

<html>
<head>
<title>mostlikers</title>
<link rel="stylesheet" href="st.css" />
</head>
<body>
<div class="mostlikers-top">
<a href="http://karthickinfotech.blogspot.in/">
<img  name="mostlikers" style="border:none;" src="demo.png" title="mostlikers"
  alt="mostlikers" /></a>
</div>
<div class="login">
<div class="addnew">
<form name="add" method="post">
<h2>Add New Record</h2>
<p><label>Title</label><br />
<input type="text" size="40" name="title" title="title" /></p>
<p><label>Descriptions</label><br />
<textarea name="desc" rows="5" cols="30"></textarea></p>
<input type="submit" name="ADD" class="submit" value="ADD" title="ADD" />
</form>
</div></div>
</div> <div class="mostlikers-bottom">

</body></html>

CSS
#resuil-title {color:#1a0dab;font-weight: bold;
font-style: normal;  }
.login form { width:600px; height:0 auto;  margin:0 auto; } 
.search { border: 1px solid #ccc;width:572px;min-height: 30px;
padding: 4px 20px 4px 8px;font-size: 18px;-moz-transition: all .2s linear; 
-webkit-transition: all .2s linear;transition: all .2s linear;}
.search:hover { width: 572px;border-color:#999999;}       
.search:focus { width: 572px;border-color:#03F;outline: none;}
.but{ padding: 6px 10px;color: #222;border-radius:3px solid #00F;
border:1px solid #CCC;background: #f4f4f4;margin-bottom:4%;font-weight:bold;cursor:pointer;
font-size:75%;font-family: -webkit-border-radius: 3px;
border-radius: 3px ;  }
.but:hover { padding: 6px 10px;color: #222;background: #f4f4f4;margin-bottom:4%;
cursor:pointer;font-size:75%;border-color:#f7f7f7;font-family: arial,sans-serif;
-moz-border-radius: 3px ;-webkit-border-radius: 3px; border-radius: 3px ;  }
.but:before { content: '🔍'; } 
.lang a{ font-weight:100px;text-decoration:none;}
.highlight { font-weight:bolder; color:red; font-style:bold; }

Update post : http://www.mostlikers.com/2016/05/create-your-own-search-engine-using-php.html

7 comments:

  1. simple, but excellent code. thanks for sharing...

    ReplyDelete
  2. hi.. nice sharing.. btw, what is the mysql query for exact search? like i want to search for 2001 so i have to type 2001 and the data will retrieve, instead of search 20 and all the data that contain 20 will be retrieve. tqsm

    ReplyDelete
  3. good work! how to make hyperlink to make fetch the data .

    ReplyDelete
  4. please help me anyone when i click on search or hit enter results show as usual but
    search queries (search keywords) does not show in url bar url remain same as(localhost/Mostliker/index.php) but i want like this (localhost/Mostliker/index.php?value=mostliker) bar as shown in other search engines like google please admin or any other member help me i am new in programming please.....

    ReplyDelete
    Replies
    1. Change your form action method post to get

      Delete