PHP Search Engine

Simple PHP Search Engine Script Tutorial

Generally dynamic websites hosts tons of data with them. Searching for a particular thing is very tedious task if websites don't have proper searching functionality. For such requirement this search tool is perfect if you want a quick and easy way to search your dynamic websites.
Having a search feature on a dynamic website is very handy for helping users find exactly what they are looking for. Almost all users now prefer different search engines to find what they are looking for. Search engines range from something very simple to very complicated as Google, MSN, Bing, Yahoo etc.

In this tutorial, our scope is limited to searching a simple database table and extract those results from database. I hope it gives a little idea to build bigger search engine for developing more complex search engine like Google.

Step 1:
Creating a database called "search-engine" and table called "mobiles".
CREATE DATABASE search-engine;
CREATE TABLE IF NOT EXISTS `mobiles` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`type` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
INSERT INTO `mobiles` (`id`, `name`, `type`) VALUES
(1, 'Samsung Wave', 'Bada OS'),
(2, 'Samsung Note', 'Android'),
(3, 'Nokia Lumia', 'Windows OS'),
(4, 'IPhone', 'IOS'),
(5, 'Motorola', 'Android'),
(6, 'Samsung Galaxy', 'Android');

Step 2:
Now create a search form (index.php) for the users where they start searching for their desired content.
<!doctype html>
<html>
<head>
<title>Simple PHP Search Engine - phphunger.com</title>
</head>
<body>
<form action="search.php" method="get" >
Please search here:
<input type="text" name="query" id="text" />
<input type="submit" name="submit" id="search" value="Search" />
</form>
</body>
</html>

Step 3:
The real search script where the real search functionality happens. Name it as (search.php) or whatever it may be. See the below code snippet.
<!doctype html>
<html>
<head>
<title>Simple PHP Search Engine - phphunger.com</title>
</head>
<body>
<?php
mysql_connect("localhost", "root", "") or die("Error connecting to database: ".mysql_error());
mysql_select_db("search-engine") or die(mysql_error());
$query = $_GET['query'];
$min_length = 3;
if(strlen($query) >= $min_length)
{
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
echo "<table border='0' width='300' align='center' cellpadding='1' cellspacing='1'>";
echo "<tr><h3>You have searched for $query...Please find the details of $query...</h3></tr>";
echo "<tr align='center' bgcolor='#03acfa' > <td height='35px' width='150px'><b>Mobile Name</b></td> <td><b>Mobile Type</b></td></tr>";
$result = mysql_query("SELECT * FROM mobiles WHERE (`name` LIKE '%".$query."%') OR (`type` LIKE '%".$query."%')") or die(mysql_error());
if(mysql_num_rows($result) > 0)
{
while($results = mysql_fetch_array($result))
{
echo "<tr align='center' bgcolor='#93dafb'><td height='25px'>".$results['name']."</td> <td>".$results['type']."</td></tr>" ;
}
}
else{
echo "<tr align='center' bgcolor='#fdee03'><td colspan='2' height='25px'>Sorry..No results for $query</td><tr>";
echo "</table>";
}
}
else{
echo "Your search keyword contains letters only ".$min_length;
}
?>
</body>
</html>

No comments:

Post a Comment