Dale Hay . com
Menu
Home
About
Contact
PHP Tutorials
BBCode
For Loop
Image Uploader
Message Board
Search Engine
Session Login
vidDetect
Photoshop Tutorials
Easy Clipart Style
Tech Stuff
HTML Friendly
MD5 Encrypter
SHA1 Encrypter
TGP Generator
Video Downloader
Web Affiliates
Web Linkbacks
Web Spam Emails
Random
Utilities
Learn Piano
Twinkle, Twinkle

PHP Search Engine Tutorial

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` (
`id` INT NOT NULL AUTO_INCREMENT ,
`pageurl` VARCHAR( 255 ) NOT NULL ,
`pagecontent` TEXT NOT NULL ,
PRIMARY KEY ( `id` ) 
) ENGINE = MYISAM 
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">
	<option>10</option>
	<option>20</option>
	<option>50</option>
</select><br>
<input type="submit" value="Search">
</form>
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
<?php
mysql_connect("localhost", "USERNAME", "PASSWORD");
mysql_select_db("DATABASE");
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.
?>
<hr>
<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">
</form>
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
<?php
mysql_connect("localhost", "USERNAME", "PASSWORD");
mysql_select_db("DATABASE");
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>";
?>

Finished

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

Feedback

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

10th Aug 2008 11:14

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

17th Sep 2008 13:46

really very good and simple
By 3m masr

29th Sep 2008 1:05

nice tut!
By Kenneth

29th Sep 2008 13:19

AWEZOMENEZZS Dale Hay . com Dale Hay . com Dale Hay . com Dale Hay . com Dale Hay . com Dale Hay . com Dale Hay . com Dale Hay . com Dale Hay . com Dale Hay . com Dale Hay . com Dale Hay . com Dale Hay . com Dale Hay . com Dale Hay . com Dale Hay . com
By APAN

30th Sep 2008 11:15

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 20:43

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

25th Oct 2008 8:34

Best places to play, best strategies to win. Player ratings & reviews of top online casinos & pokers with highest payouts & biggest bonuses.
By Rogger Mill

5th Nov 2008 1:56

:P thankss :D very much
By Dardan

9th Nov 2008 20:13

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,
Patrick
By Patrick

11th Nov 2008 20:42

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):

<?php

//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

Post your comment...

Your Name:

Your Email:

Your Website URL: (With 'http://')

Posting Codes
[b]Hello[/b] makes Hello
[i]Hello[/i] makes Hello
:-) makes Dale Hay . com
:-( makes Dale Hay . com
:-D makes Dale Hay . com
:wtf: makes Dale Hay . com
;-) makes Dale Hay . com
:mad: makes Dale Hay . com
:omg: makes Dale Hay . com
:haha: makes Dale Hay . com

Your Comment:

Enter This Number:

(70098)

Note: Your IP address is saved into the database when you submit a comment. Any type of threatening behaviour will result in your ISP being contacted and legal action being taken place!

Links
Audio Video Cables
My Other Sites
Sound Upload
UKtInfo
Budz
Feminine Hijinx
Martyn Coleman . com
SurfiOnline
Total Talent
Linux HOWTOs, Tutorials & Projects with Adam Palmer
Programs
dhSpy
Dale's Converter 1.1.1
DuMP
Other Links
Old Host: Servage
Latest News Stories
»
» No on 8!
» Atherstone Power Cut
»
» Happy 21st Birthday to Me!!
» Obama Wins
Top Viewed Stories
» Bypass Photobucket (22,109)
» Rare McDonalds Monopoly 2008 Tickets (7,915)
» PHP Mini Message Board Tutorial (5,779)
» PHP Search Engine Tutorial (3,244)
» Helen Willetts Pregnant? (2,383)
» QI Series 6 on BBC One (1,504)
Most Commented Stories
» PHP Mini Message Board Tutorial (43)
» Create your own Image Uploader (18)
» I like my DuMP (15)
» PHP Search Engine Tutorial (10)
» I'm on Wikipedia! (7)
» Chocie Munchin Niece (6)



Copyright Dale Hay, © 2005 - 2008