Php Search Engine Script for Mysql Database

The search engine script contains everything you need to build your own search engine, you have the choice of building a php-based or a perl-based search engine. I used the perldoc’s (merlyn) as my guide in building this program. The program allows you to build your own search result pages, with the title of your choice and html body of your choice. It also allows for multiple term searches simultaneously so that you can do phrase searches and is fully customizable for any web site!

A search engine script is a program that searches files and folders for the user. There are many offered by different companies but the basic functionality remains the same. When you do a search, most scripts will return the filename and location of the listed file or directory where that file is located.

Create the Search Engine Form

<form action="" method="GET" name="">
	<table>
		<tr>
			<td><input type="text" name="k" value="<?php echo isset($_GET['k']) ? $_GET['k'] : ''; ?>" placeholder="Enter your search keywords" /></td>
			<td><input type="submit" name="" value="Search" /></td>
		</tr>
	</table>
</form>

The important code to note from the form is all in the form attributes and the text box attributes:

  • action=”./index.php” – the form’s action is the end goal destination. This will be the location of the script that is going to do all the code crunching. For our search engine, index.php will be the only file we are working with. And because of that we will make the script send the url request back to itself.
  • method=”GET” – the GET method type is what will take the text entered into the text box and throw it into the url for the user. Like when you see “search.php?k=php+tutorial” (with the plus sign being a place holder for a space). That is a GET request being passed in the URL.
  • input type=”text” – the “text” type is simply going to do that, make the input a text input (crazy I know).
  • name=”k” – the “name” can be whatever you want it to be. Do keep in mind it is going to show in the users browser, like the example above. So usually shorter url is better.
  • value=”” – having a value of “” (blank) will make the text box always be empty when the user first sees it. It’s not really required, but it does make things a bit cleaner.

Make a CSS file and define styling

We make a CSS file and save it with a name search_style.css

body
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:100%;
 font-family: "Myriad Pro","Helvetica Neue",Helvetica,Arial,Sans-Serif;
 background-color:#F2F2F2;
}
#wrapper
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:995px;
}
#wrapper h1
{
 margin-top:50px;
 font-size:45px;
 color:#585858;
}
#wrapper h1 p
{
 font-size:18px;
}
#search_box input[type="text"]
{
 width:450px;
 height:45px;
 padding-left:10px;
 font-size:18px; 
 margin-bottom:15px;
 color:#424242;
 border:none;
}
#search_box input[type="submit"]
{
 width:100px;
 height:45px;
 background-color:#585858;
 color:white;
 border:none;
}
#result_div
{
 width:555px; 
 margin-left:220px;
}
#result_div li
{ 
 margin-bottom:20px;
 list-style-type:none;
}
#result_div li a
{
 text-decoration:none;
 display:block;
 text-align:left;
}
#result_div li a .title
{
 font-weight:bold;
 font-size:18px;
 color:#5882FA;
}
#result_div li a .desc
{
 color:#6E6E6E;
}

That’s all, this is how to create your own search engine using using PHP, jQuery and MySQL. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Create the Search Engine Database Structure (using MySQL/phpMyAdmin)

Next we will setup the back-end SQL database structure for the search engine. Since our search engine is simple, our MySQL database will also be simple. Just a few fields to store our data. To create the database and table, I like using phpMyAdmin to manage the MySQL database backend.

Database fields we will need

The following is the list of the MySQL database fields we will end up needing for our search engine:

  • a unique id fields to easily access the information in the database
  • the title of the site the user can search
  • the url that it will direct them to
  • a nice little blurb or description about what the site does
  • and the keywords that can be used to search for individual records

It is important to note that the id field must be set to auto increment and primary key. Doing this allows for us to have an auto incrementing unique identifier for each record in the database. It also allows the queries we run against the database to be much faster.

Make a Simple Search Engine - basic database setup

Now that the front-end and database is ready, we can move onto the fun part of the actual search engine scripting. The code to operate the search engine is going to come in a few parts. First we need to get the keywords that the user searched for and format them for our use. Then we can connect to the database and run the query. And lastly we can display the search results back to the user.

Display Search Engine Results to User

Now that we have all of our database results we can start displaying those results to the user. For the ease of the HTML, we will display 5 attributes of a product: name, brand, url, vendor and price. Using a while loop, you are able to parse through each and every one of the results returned from our search query we made earlier. The $runrows variable stores each individual record on each pass though of the loop. These results are stored as an associative array that we can process or display. Basically what this means is that we will be able to handle/format/display each of the results that are returned on a one-by-one basis. Each being stored in the same $runrows variable.

The Database Schema

Just a single table is required for the engine’s operation. The table, code, serves as the code repository. Each example is stored along with a suitable title and the chapter number in which it appears. Because the search engine should retrieve examples based on keywords found in the example title or in the code itself, a FULLTEXT index has been added for these columns. Because the table contents will rarely change beyond the occasional bug fix, its backed by the read-optimized MyISAM storage engine. The table follows:

CREATE TABLE code (
 id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
 title VARCHAR(50) NOT NULL,
 chapter TINYINT UNSIGNED NOT NULL,
 code TEXT NOT NULL,
 FULLTEXT (title,code)
) TYPE = MYISAM;

Conclusion

Why waste your time? – No monthly fees to pay. Just purchase, download and install! Compatible with all browsers. You can use this PHP search engine free of charge on any website or blog that you have. For example, search “what is php”?

0 Comments

No Comment.