How To Create Your Own Seek Engine With Php And Mysql

Posted on

Search engine has grow to be a

useful tool in ultra-modern internet international. It is very helpful for builders,

testers, managers and different net customers. Using this we are able to get the

data on any topic of our desire. In this newsletter, we will talk approximately

growing our very own seek engine with the assist of PHP and MySQL. Our goal is not

to replace the huge giants e.g. Google, Yahoo and so on however to offer a great strive in

order to have our personal seek engine. In this text we can talk approximately the

basics of search engine after which see how to develop our very own seek engine using

PHP and MySQL.

What is a search engine?


search engine is an internet-primarily based tool which permits the net users to find data

on the net. Most generally used engines like google are Google, Yahoo!, MSN,

Bing, Ask and so forth. Search engines are special forms of applications used to search

files having detailed key phrases and returns a

list of documents wherein the key phrases are positioned. Asearch engine is mostly a general collection

of programs. However, the time period ‘seek engine’

is frequently used to normally describe the not unusual systems like Google, Bing and

Yahoo! Search engines generally use automatic software packages e.g robots or spiders which movements throughout the Web

and follows the links from web page to web page, web site to web page. The information amassed

by way of those crawlers is used to create a searchable index of the Web.

How do serps work?


seek engine used is based totally on several complex mathematical formulae used to

generate the search consequences. The results received for the particular question are

then displayed at the SERP or the Search Engine Results Page. Search engine

algorithms fetch the important thing factors from the net page which incorporates the web page

title, content material and the keyword density. It then comes up with a rating primarily based

on which the consequences are positioned on the pages. Each and each search engine has

its very own specific set of rules. Hence a search end result which has a top ranking on

Yahoo does not assure a comparable rating on Google and vice versa. These

search algorithms that are utilized by search engines are

  • very intently guarded secrets,
  • in addition they continuously undergo modification

    and revision.


the criteria to nice optimize a site should be checked via consistent statement,

together with several tries no longer just once, but must be completed continuously.

It is

usually said that the much less reputed search engine marketing (Search Engine Optimization) firms hype,

since the answer to higher website online ratings will paintings at its best for best a restricted

duration till the time the developers of the search engine turn out to be smart enough to

the processes and change their algorithm. Normally web sites using these hints are categorized

as unsolicited mail with the aid of the search engines and hence, their ratings go down.


engines most effective check the texts on net pages, and use the underlying HTML

structure to be able to find out the relevance. Images, photographs, or dynamic Flash

lively contents are meaningless to serps; but, the real text

content is more applicable for the engines like google. It is a hard venture for

the flash builders to construct a Flash website so that it will be friendly to the quest

engines. As a end result, the flash sites will generally tend now not to rank as high compared

to sites which might be developed with properly coded HTML and CSS (Cascading Style

Sheets — a complicated mechanism for adding patterns to website pages above and

past ordinary HTML). If the phrases, we

are trying to be found via do not seem within the text of our internet site, it will likely be

very tough for the website to produces a high placement at the Search Engine

Result Pages.

Web Search Engines


normally check with Web Search Engine at the same time as speakme about Search Engines. A traditional

Web Search Engine begins working through sending out a spider which

has the capability to fetch as many documents as viable towards the provided

keywords. There is another program referred to as the indexer, and then starts studying these documents and begins developing

the indices based at the tokens determined in every document. Each seek engine uses

its very own proprietary set of rules to create the indices in this type of way that preferably,

only meaningful results are returned for every query.


nearly every internet site proprietors rely upon the truth that the search engines will send

visitors to their website and additionally the whole industry has grown across the concept

of optimizing Web content material to be able to improve their placement in the seek

engine effects, we must acquire good enough information approximately seek engine

optimization or search engine marketing.

Characteristics of Search Engine

  • Unedited – Search engines are unedited.

    Anyone can enter the search content.

    • Some serps come up with inbuilt quality troubles;
    • Search engines can mark the websites as Spam if the websites uses a few

      tricks so that it will be at the top of the SERP.

  • Search engines comes up with an array of statistics kinds e.g. Phone

    e book, brochures, catalogs, dissertations, news reports,

    weather, multi function location!

  • Search engines have the potential to cater the wishes of various types of


Tips to be observed whilst Using Search Engines

We have to

make certain to be known as as one of the fine web searcher that something we need to type

so as to tell the quest engine have to be exactly what we are searching out. The

seek operators fall into huge categories –

  • Boolean operators and
  • Non-Boolean operators


operator is a word or image which we kind. It offers the quest engine the instructions

assist it is aware of what to search for. Using these operators we can either slender or

expand our seek, for that reason supporting us to discover the net websites which may be beneficial

to you.

We need

to check out the character search engines like google and yahoo to find out which operators it’s far

primarily based upon.


Searching Techniques:

  • AND

    – This means the quest outcomes should have each the phrases. Very regularly it is

    typed in UPPER CASE, however is not obligatory. This reduces the matter of the

    web sites. Thus narrowing in on precise subjects.

    e.g. air AND water AND pollutants will search for the internet web sites which comprise all the three keywords – air, water

    and pollutants

  • OR

    – This manner that the search outcomes might also have either of the terms that is

    entered. This increases the number of internet web sites so as to be appeared upon, thus

    broadening our search. E.g. air OR pollutants

    OR water will search for the internet sites that contains both air or pollution or


  • NOT

    – This means any result containing the second one key-word or the token may be

    excluded from the search result. Using NOT decreases the number of sites that

    may be indexed due to our search. Thus it narrows our search. e.g. if

    we input the tokens – air NOT pollution, the consequences will listing out the websites

    that contain air but now not pollutants.

We must be very cautious approximately whilst

NOT because if a internet site mentions pollutants even as soon as, it can be excluded from

the search result. This should cause exclusion of a few essential web sites. Similarly

we ought to be careful even as the usage of OR. as this can turn out to be with a huge range of

websites which require sort via.

Non-Boolean Searching Techniques:

  • +

    This works inside the identical manner the AND works. This makes the token mandatorily required

    in the seek consequences. The image + need to be positioned at once in front of the

    seek token and should now not have any spaces. e.g. air+water+pollution will look

    for all the internet websites that contain those 3 tokens – air, water and pollutants.

  • This works within the same manner the NOT works. This is supposed to exclude the token

    which follows the image -. e.g air – pollutants

    will list out all the websites that comprise air but now not pollution.

  • ” This symbols is placed around the tokens to signify that the search engine

    will look for the precise phrase. e.g. “air water pollutants” will look for that

    exact word. This will make your seek very unique.

Similar to the Boolean terms, we

must be careful even as the usage of – as this may remove the internet websites that might

mention the term which we do now not need, however are not virtually about that time period. –

Water will cast off all of the internet

web sites which might be about air pollution but communicate about water pollutants as well.

Creating our very own engines like google


tired of the usage of the accepted engines like google e.g. Google and Yahoo whenever, we

want to appearance out for some thing. Best concept might be to try to build our own

Search Engine the usage of the open supply technologies like PHP and MySQL. Let me

make it clear that our goal isn’t always to throw the big giants e.g. Google, Yahoo,

Bing and many others out of the marketplace, but we can deliver an amazing attempt to have our own

search engine.

In this

approach we will discover ways to construct our own seek engine and finally we would see

the traffic coming and doing a seek on our internet site in mass with the assist of

an Html seek form, having the same old button. Here we will use the Hypertext Preprocessor

language and MySql database services if you want to implement this feature. Hence

it’s miles anticipated from the readers to have a terrific understanding of the primary principles

of both of those before pass and begin imposing. In this document, we will use

the most simple code and no longer go through the complicated square queries. Here, we are able to expect

that the fundamentals of Structured query language or SQL is understood to all people and also you

have been the usage of it in some form or different more regularly. So now let us cognizance on our

first HTML code as a way to help us to create a Search Button and form that’s

going to be utilized by every traveler to enter in any question.



As we

have noted that MySQL is one of the prerequisite in our technique, our first

step might be setup the MySQL database up and walking. Connect to MySQL, we are able to

any use any of the UI based totally unfastened gear e.g. Squirrel, HeidiSQL or DBVisualiser

or the MySQL admin console. Once related, let run the following SQL which

will create a desk referred to as SEARCH_ENGINE.

Listing 1: An SQL declaration in an effort to create a desk –

CREATE TABLE SEARCH_ENGINE ( `identification` INT(11) NOT NULL AUTO_INCREMENT, `pageurl` VARCHAR(255) NOT NULL, `pagecontent` TEXT NOT NULL, PRIMARY KEY (`identification`))


above question will create a table in the database if you want to be used to shop the

information or records to be stored in the database.


the Form:


as soon as the database is prepared, allow us to make the form so one can be utilized by the

traffic or the cease users to perform their seek. Let us name this report – ‘index.Hypertext Preprocessor’

which is a simple seek bureaucracy having a button. Here we will use GET in place of

POST. Thus the facts is made pretty seen in the address bar.

Listing 2: Our index.Hypertext Preprocessor file –


<html> <head> <title> My search engine </title> </head> <body> < form movement = 'seek.Hypertext Preprocessor' method = 'GET' > < center > <h1 > My Search Engine </h1 > < enter type = 'text' size='ninety' name = 'search' > </ br > </ br > < input type = 'post' name = 'post' price = 'Search source code' > < choice > 10 </ option > < option > 20 </ option > < option > 50 </ choice > </ middle > </ shape > </ body >

</ html >

Our form

is now completed and geared up to be used. This form can be used by the stop customers to

enter in a query and at the same time will enable the customers to restriction the be counted

of results which needs to be proven at the shape.


the Query:

Let us

create a brand new document ‘search.personal home page’ that’s the web page in which the consequences from the

search can be listed or shown. This report is split into following sections –


Let us connect to the database first:

Listing 3: DB connection

mysql_connect ( "localhost", "USER_NAME", "PASSWORD" ) ; 
mysql_select_db ( "DB_NAME" );


Form the query – Once we’re connected to the DB, we

then form the query the usage of the tokens that the end customers have entered. This is

shown underneath –

Listing four: Construct the query along with the tokens users

have entered –

$search_exploded = explode ( " ", $seek );
$x = zero; 
foreach( $search_exploded as $search_each ) 
$assemble = " ";
if( $x == 1 )
$construct .= "keywords LIKE '%$search_each%' ";
$assemble .= "AND key phrases LIKE '%$search_each%' ";

$assemble = " SELECT * FROM SEARCH_ENGINE WHERE $assemble ";
$run = mysql_query( $construct ); 


Our next task is

to fetch the outcomes from the database and gift it to the person. If the

seek would not yield any end result, we should display an appropriate message to the

consumer as proven below –

Listing four: Fetch the result and gift it to the consumer –

if ($foundnum == 0)
echo "Sorry, there are no matching end result for <b> $seek </b>.
</ br >
</ br > 1. Try extra wellknown phrases. for example: If you want to search 'how to create a internet site' then use trendy key-word like 'create' 'website'
</ br > 2. Try specific phrases with similarmeaning
</ br > three. Please test your spelling"; 
echo "$foundnum consequences discovered !<p>";
at the same time as ( $runrows = mysql_fetch_assoc($run) ) 
$identify = $runrows ['identify'];
$desc = $runrows ['description'];
$url = $runrows ['url'];
echo "<a href='$url'> <b> $identify </b> </a> <br> $desc <br> <a href='$url'> $url </a> <p>";


Now our Search engine is ready to be used.

The code defined above in parts is listed below –

Listing 5: The Complete Search.PHP report –

<?personal home page $button = $_GET [ 'put up' ]; $seek = $_GET [ 'seek' ]; if( !$button ) echo "you failed to put up a keyword"; else if( strlen( $search ) <= 1 ) echo "Search time period too quick"; else echo "You looked for <b> $seek </b> <hr length='1' > </ br > "; mysql_connect( "localhost","USERNAME","PASSWORD") ; mysql_select_db("DBNAME"); $search_exploded = explode ( " ", $seek ); $x = 0; foreach( $search_exploded as $search_each ) $x++; $assemble = ""; if( $x == 1 ) $assemble .="keywords LIKE '%$search_each%'"; else $construct .="AND keywords LIKE '%$search_each%'"; $construct = " SELECT * FROM SEARCH_ENGINE WHERE $assemble "; $run = mysql_query( $construct ); $foundnum = mysql_num_rows($run); if ($foundnum == 0) echo "Sorry, there are not any matching end result for <b> $search </b>. </br> </br> 1. Try greater trendy words. as an example: If you need to go looking 'the way to create a website' then use general key-word like 'create' 'internet site' </br> 2. Try different words with similarmeaning </br> three. Please test your spelling"; else echo "$foundnum outcomes determined !<p>"; while( $runrows = mysql_fetch_assoc( $run ) ) $identify = $runrows ['name']; $desc = $runrows ['description']; $url = $runrows ['url']; echo "<a href='$url'> <b> $title </b> </a> <br> $desc <br> <a href='$url'> $url </a> <p>"; ?>

Search Engine architecture


going into in addition info, let us communicate approximately what ought to be our dreams whilst

growing a search engine. Listed under is a brief set of dreams which we

must be focused on –

  • WebCrawler, indexer and record garage

    which must be capable of handling a large volume of documents can be 1

    million or even more. .

  • We have to follow the take a look at driven

    development which might help to put into effect top layout and modular code.

  • We should have the potential to aid numerous

    strategies for things like the index, file store, seek and so forth.

A ordinary

seek engine consists of few parts –

  • A crawler which is used to pull outside


  • An index which is the area where the

    files are stored in an inverted tree and

  • A file store to keep the documents.



order to crawl, we ought to give you a list of URL’s. There are a few widespread

approaches to do this as indexed below –

  • The maximum commonplace is to feed the crawler

    with a listing of hyperlinks which include masses of hyperlinks as indexed. Our subsequent task is to crawl

    them and harvest as we cross down the listing

  • Another approach is to down load a listing of URL’s

    after which use that listing.


our purpose is to get the real internet site simplest, allow us to write a easy parser to extract

the best records out. It is quite straight forward as proven under –

Listing 6: The parser –

$file_handle = fopen( " Quantcast-Top-Million.txt ", "r" );

at the same time as ( !feof ( $file_handle ) ) 
$line = fgets( $file_handle );
if( preg_match( '/^\d+/',$line ) )  # if it starts with some amount of digits
$tmp = explode( "\t",$line );
$rank = trim( $tmp[0] );
$url = trim( $tmp[1] );
if( $url != 'Hidden profile' )  # Hidden profile seems now and again simply ignore then
echo $	

fclose( $file_handle );



the data is going to make the effort consequently we have to be prepared for an extended wait.

We can write a very simple crawler in PHP surely by using the use of a file_get_contents

and sticking in a url. Let us have a check out the following code –

Listing 7: The crawler –

$file_handle = fopen("urllist.txt", "r");
even as (!feof($file_handle)) 
$url = trim(fgets($file_handle));
$content = file_get_contents($url);
$record = array($url,$content material);
$serialized = serialize($document);
$fp = fopen('./documents/'.md5($url), 'w');
fwrite($fp, $serialized);



above code is basically a single threaded crawler. It clearly loops over each

url within the document, extracts down the content material after which saves the content to the disk.

The only element we must notice here is that it shops the url and the content in

a file considering that we’d need to to use

the URL for ranking reason and also it’s miles useful to maintain a track where the

report got here from. We have to keep in mind that we may additionally run out of report gadget garage limits even as trying to

save masses of files in a single folder.



cause I first of all mentioned the take a look at pushed development mechanism, is that I

decide upon the bottom up approach. The index, which we are going to create, need to

have a few quite simple obligations as indexed underneath –

  • It needs to save its contents to disk and

    retrieve them.

  • It needs so one can clear itself while

    we determine to regenerate things.

  • It have to validate files that its



those responsibilities defined Let us have the subsequent interface in place –

Listing 8: The interface –

interface iindex 
public function storeDocuments($name,array $files);
public feature getDocuments($call);
public characteristic clearIndex();
public characteristic validateDocument(array $report);



report store is a fairly strange if we are going to index things that we likely

already have what we desired to be saved somewhere else. The maximum apparent factor

in this example is that the documents are already in a few database.



subsequent step in our method to build our search is to create the indexer. An

indexer takes a record, breaks it apart and feeds it into the index, and additionally

probable to the record keep relying upon our implementation.



that we have the capability to store and index a few documents. Let us go through

the stairs we want right here to have the indexing in location –

  • The first factor we’re presupposed to do here

    is to set the time restriction to unlimited because the indexing process might take a

    longer time than predicted.

  • Our next step is to outline the placement of

    the index and the documents which might be going to stay as a way to keep away from the mistakes.



requires a fairly easy technique. In truth we best require a unmarried method

as proven under –


nine: The search interface –

interface isearch 
public characteristic dosearch($searchterms);


direction, the actual implementation isn’t that smooth. It is rather greater complicated

than it appears.



this file, I actually have tried to cowl the unique regions of search engine and

its functions. Also I actually have discussed on how to create our personal search engine with

the help of MySQL and. Let us finish this article in following bullets –

  • Search engine is a effective and useful

    tool in today’s Internet global.

  • A search engine is primarily based on numerous complex

    mathematical formulae which might be used to generate the quest results.

  • The consequences acquired for the unique

    question are then displayed at the SERP or the Search Engine Results Page.


desire by using now you’ve got were given an amazing idea of JDB or Java Debugger and additionally wish that

you have loved studying this file.

Kaushik Pal


Have sixteen years of experience as a technical architect and software representative in agency software and product improvement. Have hobby in new generation and innovation vicinity together with technical…


Thank you very a good deal for this super article. I was looking for search engine and crawler script from last 1 month. I am now not professional in Hypertext Preprocessor so I need to request you that please provide an explanation for from Listing 6: The parser – this section. In which record I want to add coding from Listing 6 to Listing 9. Or do I want to create every document for each list from 6 to nine.


[+1 year ago]    Answer it


helo friend,first-rate article on www. I too am diversifying and seek engine is first-class subject,my question is shall i consist of parser.Hypertext Preprocessor and crawler.personal home page within search.php?

[+1 12 months in the past]    Answer it

Mr.Bool Editor


This is as much as you. It’s flawlessly great. 🙂

10/1/2015 2:7pm   Answer it

satish deuba

Thanks kaushik sir it helped me plenty to apprehend a way to make it and well written too and as soon as thank you very much

[+1 12 months in the past]    Answer it

Goutham Reddy

Hi. I even have tried to create seek engine web page, however failed to connect thru neighborhood server. How did you implement the code? Any one help me?

6/22/2015 five:54am   Answer it

Manojit Ballav

The SQL instructions right here are deprecated , so that you will must use a new set of MYSQL commands

1/14/2016 zero:4am   Answer it


i were given an mistakes :

Warning: mysql_num_rows() expects parameter 1 to be aid, boolean given in C:\wamp\www\firstsearch\search.personal home page on-line 37 how to remedy it ?

[+1 yr in the past]    Answer it

Makmesh IKiev

Thanks for this exceptional submit.

You have an mistakes on Listing 4: Construct the question in conjunction with the tokens customers have entered

this line: $construct = ” “; ought to be outdoor the foreach loop.

[+1 yr ago]    Answer it

Goutam Reddy

please help me sir

mistakes >>

mysql_num_rows() expects parameter 1 to be resource, boolean given in E:\xampp\htdocs\SE\seek.php on line 27


and please offer me extra resources ..

[+1 year in the past]    Answer it

Daniel Aguilar

Hi, can you give an explanation for the way to add the parser and the crawler ? I brought a urllist textual content record, but can’t discern out a way to upload the ones, like an encompass page? of crawler.personal home page or parser.personal home page, and so on.

[+1 yr ago]    Answer it

Dheztyvanesh Axelia

Welcome to the net Agent Online, We are marketers on line playing who have years of experience in serving bettors Online (Ball Betting, Poker, Togel,Casino and agile)

– SPECIAL PROMO 20% deposit bonus SPECIAL NEW MEMBER SportBooks

– 15% Cashback Bonus

– Reference friend Bonus 2%

– Rolingan zero.7%

Soon join us : , , , ,

[+1 12 months ago]    Answer it

Dheztyvanesh Axelia

This is a superb site! come on and be a part of with us! , , , ,

[+1 year in the past]    Answer it

jaafar azzizi

now there is a great way to create your search engine, Hypertext Preprocessor search engine script <a href=”” rel=”dofollow”></a>

[+1 12 months ago]    Answer it

Leave a Reply

Your email address will not be published. Required fields are marked *