minda net[my joomla! minda!]

  • Increase font size
  • Default font size
  • Decrease font size
Home

How to Create your own search engine using Php and MySql Database - For your Website

E-mail Print PDF
User Rating: / 0
PoorBest 

Method

In this tutorial, You will learn "How to build your own search function" for allowing your visitors to search through your site via a Html search form or Button. Here, we are using php language and MySql database services to develop this functionality and is recommended to clear the basic concepts about both before reading more. I unexpectedly developed this when I'm helping one of my colleague and friend in completing his college assignment, So, I will provided you here with the most basic code you can use to do this task. You can also do this task in other languages but in that also you need to know the raw (most basic) sql queries and how to implement them in a particular language. I suppose you know the basics of Structered query language and are already using them in your codes that were written for your site. So here's the first HTML code to create a search button and form to accept user input and query.
<form action="search.php" method="get">
<label>Search For: </label><input name="query" type="text" />
<input value="Start Search" name="submit" type="submit" />
<input value="Reset" type="reset" /></form>
Paste the above code anywhere in your page within the body tag and can customize it's look via CSS. Now, here is the snippet of php code that will retrieve results from MySql database and I'm sure you never assumed this to be so easy. Save this code on another page with name "search.php" in the same directory or you can do changes as per your requirement. Here, $_GET['query'] fetches or decodes the data provided via form from any webpage using 'GET' method as a parameter value of url or webpage address,
<?php
// Change the fields below as per the requirements
$db_host="Host Name";
$db_username="Username used to access database";
$db_password="Password to access database";
$db_name="Database u created for site";
$db_tb_name="Name of the table u created in site database";
$db_tb_atr_name="attribute(column name) of table in which search action is to be performed";
 
//Now we are going to write a script that will do search task
// leave the below fields as it is except while loop, which will display results on screen
 
mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");
 
$query=mysql_real_escape_string($_GET['query']);
 
$query_for_result=mysql_query("SELECT * FROM $db_tb_name WHERE 
 
$db_tb_atr_name like '%".$query."%'");
echo "<h2>Search Results</h2><ol>";
while($data_fetch=mysql_fetch_array($query_for_result))
{
    echo "<li>";
    echo substr($data_fetch[$db_tb_atr_name], 0,160);
    echo "</li><hr/>";
}
echo "</ol>";
 
mysql_close();
?>

Basics of Code snippet doing the search task on database you created for your website:

We are storing the changeable values in variables defined at the top of script,

Then, we are connecting and selecting the database we created for our site,

Then, we are executing the mysql query using mysql_query() function on a particular column name (attribute) of table in our site database.

Note: We are using the '%' sign on both side of '$query' variable in 'WHERE' condition of mysql query to find the matching queried word in a sentence or in between a big word stored in our database.

Then, we are running 'while loop' condition to fetch all the rows containing the giving word till the last check and

we are printing them on screen simultaneously using echo command.

Then, at last after the work is completed, we are closing the connection with database for the stability and security purposes.

Finally, the conclusion is "Script presented here contains nothing but an extra '%' and 'LIKE' commands in query which does the work of searching as according to the needs of most basic search engine with only one algorithm".

To Make it Work on Single Page (Section Added on 15:30 IST 22-09-2011) If You think that creating two separate pages for both PHP Script and HTML form is little lengthier and frustrating task, then, here is the solution of it.

You can copy and paste code shown below in a single page and

it does not require extra modifications, other than those explained above.

You can still make it work via other pages after setting the action attribute of HTML form pointing to this page.

You can Clearly see in PHP script below,

We are using the isset() function of PHP, Simultaneously with "IF" PHP statement to check submission of form.

If form is submitted, then, PHP Code will execute or vice versa.

<form method="get" action="">
<label>Search For: </label><input type="text" name="query" />
<input type="submit" name="submit" value="Start Search" />
<input type="reset" value="Reset"
</form>
 
<?php
//PHP CODE STARTS HERE
 
if(isset($_GET['submit'])){
 
// Change the fields below as per the requirements
$db_host="Host Name";
$db_username="Username used to access database";
$db_password="Password to access database";
$db_name="Database u created for site";
$db_tb_name="Name of the table u created in site database";
$db_tb_atr_name="attribute(column name) of table in which search action is to be performed";
 
//Now we are going to write a script that will do search task
// leave the below fields as it is except while loop, which will display results on screen
 
mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");
 
$query=mysql_real_escape_string($_GET['query']);
 
$query_for_result=mysql_query("SELECT * FROM $db_tb_name WHERE 
 
$db_tb_atr_name like '%".$query."%'");
echo "<h2>Search Results</h2><ol>";
while($data_fetch=mysql_fetch_array($query_for_result))
{
    echo "<li>";
    echo substr($data_fetch[$db_tb_atr_name], 0,160);
    echo "</li><hr/>";
}
echo "</ol>";
 
mysql_close();
}
?>

Finally, you have created your first search engine and can start developing algorithms to output most useful/related results on users screen with a better look.

Last Updated on Friday, 10 February 2012 17:45  

Newsflash

With a library of thousands of free Extensions, you can add what you need as your site grows. Don't wait, look through the Joomla! Extensions library today.