PHP MySQL Search Engine --Search In Database - Web Development and Web Design Codes

Latest

Wednesday, February 14, 2018

PHP MySQL Search Engine --Search In Database

PHP MySQL Search Engine

PHP MySQL Search Engine
PHP MySQL Search Engine

Hi Friends We are Going to learn how to create a Search Script using php and MySQL Database..
you can implement this search engine codes to your site..

=>Database and Table Structure:
  Database Name: codenair
 Table Name: topic

Table topic Structure:

CREATE TABLE IF NOT EXISTS `topic` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `content` longtext NOT NULL,
  PRIMARY KEY(id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Dumping Data for Table topic

INSERT INTO `topic` (`id`, `title`, `content`, `author`, `views`) VALUES
(6, 'How to Install CakePHP', 'How to Install CakePHP..How to Install CakePHP..How to Install CakePHP..How to Install CakePHP..How to Install CakePHP..'),
(5, 'How to Install Laravel', 'How to Install Laravel..How to Install Laravel..How to Install Laravel..How to Install Laravel..How to Install Laravel..'),
(3, 'How to Install windows 7', 'this is How to Install windows 7this is How to Install windows 7this is How to Install windows 7this is How to Install windows 7this is How to Install windows 7'),
(4, 'How to Install windows XP', 'This is how to Install windows xp.This is how to Install windows xp.This is how to Install windows xp.This is how to Install windows xp.This is how to Install windows xp.'),
(2, 'php mysql insert', 'This is php mysql insert tutorials.This is php mysql insert tutorials.This is php mysql insert tutorials.This is php mysql insert tutorials.This is php mysql insert tutorials.'),
(1, 'php mysql connect', 'This is php mysql connect tutorials.This is php mysql connect tutorials.This is php mysql connect tutorials.This is php mysql connect tutorials.This is php mysql connect tutorials.');

Files:
index.php and style.css

1. index.php

<html>
<head>
<title>PHP MySQL Search Engine</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
 
<form method="POST" action="">
<div id="form">
<h2>PHP MySQL Search Engine</h2>
<table>
   <tr>
     <td><span>Search For:</span></td>
  <td><input type="text" name="search"/></td>
   </tr>
   <tr>
     <td></td>
  <td><input type="submit" name="submit" value="Search Now"/></td>
   </tr>
</table>
<?php if(isset($error)){echo $error;}?>
 
</form>
</html>
 
<?php
  $conn=new mysqli('localhost','root','','codenair');
if(isset($_POST['submit'])){
 $term=mysqli_real_escape_string($conn,$_POST['search']);
 //Counting String Length
 if(strlen($term)<3){
  echo '<font style=color:red size=4>Your Search Value Should be Greater Than 3 Charactars</font>';
}else{
  $result=mysqli_query($conn,"SELECT*FROM topic WHERE title like '%$term%' or content like '%$term%'");
  $count=$result->num_rows;
  if($count>0){
   echo '<b>Your Search Result for: <font color=green> '.$term .'</font></b>';
   while($row=$result->fetch_array()){?>
     <div id="content">
       <h3><?php echo $row['title'];?></h3>
    <p><?php echo $row['content'];?></p>
      
     </div>
   <?php
   }
  }else{
   echo '<b>No Result Found for:  <font style=color:red size=4>'.$term.'</b></font>';
  }
   }
}
 
?>
</div>

2. style.css

#content{
 width:500px;
 margin:auto;
 background:#ccc;
}
#form{
 background:#ddd;
 width:500px;
 margin:auto;
}
h3{
 background:black;
 color:white;
}
input[type=text]{
 padding:4px;
 border-radius:4px;
 width:250px;
}
input[type=submit]{
 padding:4px;
 border-radius:4px;
 background:black;
 color:white;
}

That's it friends How to create search script using php MySQL ..Keep visiting ..Thank You...

No comments:

Post a Comment

Thank You for Your Comment

Note: Only a member of this blog may post a comment.