7th Aug 2008 10:46pm
66,220 views | 27 comments
Flattr this

Search Engine Tutorial

Heard of Google? or Yahoo!? ... of course you have. Also you know exactly what they do. But why should you just let Google or Yahoo or other search engines take all the glory. Why not try yourself and make your own Search Engine in PHP and MySQL. Ok, you won't beat Google.. but you can have a really good try.

Database Stuff

Open up your phpMyAdmin (or database software) and add this query below into the SQL section.
CREATE TABLE `searchengine` (
`pageurl` VARCHAR( 255 ) NOT NULL ,
`pagecontent` TEXT NOT NULL ,
PRIMARY KEY ( `id` ) 
This will allow the information to be stored in the database.

Creating the Form

The database information is now added. Now we need to make the form that allows you to search. So create a file called 'index.php'

The form will be using GET instead of POST. (So the information will be visible in the address bar)
<form action="search.php" method="GET">
<b>Enter Search Term:</b> <input type="text" name="term" size="50"> <b>Results:</b> <select name="results">
<input type="submit" value="Search">
That's the form completed. This will allow you to type in a query and also select the number of results you want to show on the form.

Processing the Query

Create a new file 'search.php'. This is the page where the results from the search being shown.

You will need to connect to the database first
mysql_connect("localhost", "USERNAME", "PASSWORD");
Now perform the MySQL query (as a query)
$sql = mysql_query("SELECT * FROM searchengine WHERE pagecontent LIKE '%$_GET[term]%' LIMIT 0,$_GET[results]");
The above query will search in the page data (that we'll be adding later on) and show it as many times as you specified. Now let's create the Array that'll show the results.
while($ser = mysql_fetch_array($sql)) {
	echo "<h2><a href='$ser[pageurl]'>$ser[pageurl]</a></h2>";
Then just add a link at the bottom that goes back to the original index page.
<a href="./index.php">Go Back</a>

Adding Information

Great! The search engine frontend is basically complete! Now we just need to make a little form that adds information into the database.

Create a new page called 'addurl.php' and create a simple form.
<form action="./addurl2.php" method="POST">
Page URL: <input type="text" name="url" size="50"><br>
<input type="submit" value="Add URL">
Now create a second page called 'addurl2.php' and this is where the information will be processed and added to the database ready to be searched.

Connect to the database
mysql_connect("localhost", "USERNAME", "PASSWORD");
The URL of the page will now get its source code read and made into a varible.
$pagedata = htmlspecialchars(file_get_contents($_POST['url']));
This little line will remove some single quotes that will usually mess up the query.
$pagedata = str_replace("'","",$pagedata);
Now to insert the information into the database.
mysql_query("INSERT INTO searchengine VALUES ('','$_POST[url]','$pagedata')");
Give a small added message, just so you know it's been added.
echo "URL Added.<br><a href='./addurl.php'>Continue...</a>";


There you go... a nice simple search engine. You can now goto 'addurl.php' and enter the URL(s) of the page(s) that you want added into your search engine. Obviously if you search for 'b' you'll get loads of results because it'll be reading stuff like '<b>'.

What you should have learned...

  • How to build a HTML form
  • How to connect to a database
  • How to read information from a database
  • How to add new information into the database

Things to make it better?

  • Modify the 'addurl.php' page to filter out certain words and/or phrases.
  • Use pagination (so you can show more than 50 results on more than 1 page)
  • Create an Admin Control Panel, so you can add, edit and delete results.
  • Password protect the 'addurl.php' and 'addurl2.php' pages to stop other people from adding information.
  • Add categories to the search (eg; news, blogs, technology, entertainment)
  • Adapt on the methods used in this tutorial to make a discussion board / forum with different rooms and categories


I'd love to hear your feedback. If you have any then please contact Dale Hay (that's me!) or leave a comment!


Tweet ThisShare On FacebookShare On Google ReaderAdd To Del.icio.usShare On DiggShare On RedditShare On LinkedInShare On StumbleUpon


10th Aug 2008 09:44

Thanks for this tutorial, it's really helpful. Only thing I was wondering about, is your thoughts on making a crawler. Let's be honest, this ain't going to be a large search engine ;)
By Koen

25th Aug 2008 15:10

By fdg

17th Sep 2008 12:16

really very good and simple
By 3m masr

28th Sep 2008 23:35

nice tut!
By Kenneth

29th Sep 2008 11:49


30th Sep 2008 09:45

If i want to remake this one to make it search for pictures, how would i do that?, tips wanted.
By gnutt3n

5th Oct 2008 19:13

hi Dale
i have used your search engine in which i think is absolutely brilliant as i was using one of those free search engine things hosted on the net before, however i have been messing about with it as when i upload a url to the database i would like to add other data e.g keywords, address, and so on could you please help me to do this by way the line break between each result works great thankyou very much

kind regards

Mark Proctor
By Mark Proctor

5th Nov 2008 00:26

:P thankss :D very much
By Dardan

9th Nov 2008 18:43

Hi Dale, and thanks for this great tutorial. It will truly be a great asset to my website when I finish it, but I have a couple of questions first of all.

On the addurl2.php page, is it possible to echo/display a list of URLs that have previously been added?

Also, how complicated can this tutorial expand to? Is it possible to show a segment of text on the results page which contains the word or phrase that the user searched for?

Thanks again,
By Patrick

11th Nov 2008 19:12

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/www/idebate.reviveddesigns.com/search.php on line 18

Here's my code (i modified yours only by changing it to use a dbConfig.php file I made):


//database connect

include ("dbConfig.php");

$sql = mysql_query("SELECT * FROM searchengine WHERE pagecontent LIKE '%$_GET[term]%’ LIMIT 0,$_GET[results]");

while($ser = mysql_fetch_array($sql)) {
echo "<h2><a href='$ser[pageurl]'>$ser[pageurl]</a></h2>";

By George Dunbar

8th Jan 2009 10:43

This is a joke. Obviously dont know php.
By lol

22nd Apr 2009 01:07

Ja det kan du. :)
By Dale Hay

4th May 2009 20:45

any updates coming ?
By Cyncbeecy

15th May 2009 04:50

what if i want to search for image?search and display the result...plez help me??
By howTo

16th May 2009 10:31

really nice. millions of thanks

8th Jun 2009 21:12

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/www/idebate.reviveddesigns.com/search.php on line 18

By LifeBurns

9th Jun 2009 00:33

LifeBurns - If you paste your code or email it me, I'll take a look for you.
By Dale Hay

12th Jun 2009 20:06

hi..currently we are only able to add URL and not the pagecontent in 'addurl.php. now how we do add pagecontent and URL in a same page rather then go to database edit manually ?? sorry for bad English Tq
By rashim

18th Jun 2009 08:41

What a bad tutorial.
it's ok for beginners i guess, but i found
to be a lot better.
By (Pyro)

22nd May 2012 06:05

Yes ... the design is clearly needed to be changed :)
What would be brighter , nebudu (
By Menelom

2nd Jun 2012 01:41

delete my topic, admins, plz
By DommerbJem

11th Jul 2012 13:58

Primarily a webspace is neeedd to host ur search engine web application filesShould be built on any of serverside programming languages like asp, asp.net, jsp, php, etc.,The web application for serach engine should contain a textbox for the text entered to be searched, a button to proceed the search for the given text in textboxa searching logic or algorithm should be implemented to the application.the same logic can be implemented on any proramming languages as mentioned above.a database is necessaryit is connected to ur application.the database should contain the texts and its corresponding linkz relevant to the searching text.mostly used databases are oracle, ms sql server, my sql, ms accessthats all. ur search engine is ready now
By Mazhar

11th Jul 2012 14:06

Howdy! I know this is kinda off topic however , I’d fieugrd I’d ask. Would you be interested in trading links or maybe guest authoring a blog article or vice-versa? My website discusses a lot of the same subjects as yours and I feel we could greatly benefit from each other. If you might be interested feel free to shoot me an e-mail. I look forward to hearing from you! Great blog by the way!
By Seresa

11th Jul 2012 17:35

Hey there would you mind letting me know which web host you’re utiziling? I’ve loaded your blog in 3 completely different web browsers and I must say this blog loads a lot quicker then most. Can you recommend a good internet hosting provider at a honest price? Cheers, I appreciate it!
By Daytonia

22nd Aug 2012 11:10

its good for beginners but for some advanced www.justiworld.com
By N Singh

4th Oct 2013 14:19

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

$sql = mysql_query("SELECT * FROM searchengine WHERE pagecontent LIKE '%$_GET[term]%' LIMIT 0,$_GET[results]");
By savi

4th Oct 2013 19:12


This should work:

$sql = mysql_query("SELECT * FROM searchengine WHERE pagecontent LIKE '%" . $_GET[term] . "%' LIMIT 0,$_GET[results]");
By Dale Hay

Post your comment...

Your Name:

Your Email:

Your Comment:

Are You Human?

Tick this box to confirm you're not a bot.

  • © Dale Hay, 2005 - 2016