PHP Search Engine Tutorial

7th Aug 2008 22:46pm

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

Things to make it better?

Feedback

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

Read and Leave a Comment 37 Comment(s) -:- Permalink
Views: 29,361 Tags: php, mysql, tutorial, search engine

10th Aug 2008 15: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

17th Sep 2008 18:16

really very good and simple
By 3m masr

29th Sep 2008 5:35

nice tut!
By Kenneth

29th Sep 2008 17:49

AWEZOMENEZZS
By APAN

30th Sep 2008 15:45

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

6th Oct 2008 1: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

25th Oct 2008 13:04

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 6:26

:P thankss :D very much
By Dardan

10th Nov 2008 0: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,
Patrick
By Patrick

12th Nov 2008 1: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):

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

8th Jan 2009 16:43

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

21st Apr 2009 23:59

God dag! Kan jag ladda ner en bild fran din blogg. Av sak med hanvisning till din webbplats!
By ChabrellIgan

22nd Apr 2009 7:07

Ja det kan du. :)
By Dale Hay

5th May 2009 2:45

any updates coming ?
By Cyncbeecy

15th May 2009 10:50

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

16th May 2009 16:31

really nice. millions of thanks
By

7th Jun 2009 1:47

i've made a search code generator.
By djjjozsi

9th Jun 2009 3: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

Why???
By LifeBurns

9th Jun 2009 6:33

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

13th Jun 2009 2: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

14th Jun 2009 2:43

The best information i have found exactly here. Keep going Thank you
By JaneRadriges

18th Jun 2009 14:41

What a bad tutorial.
it's ok for beginners i guess, but i found
http://www.roscripts.com/PHP_search_engine-119.html
to be a lot better.
By (Pyro)

8th Aug 2009 15:22

"Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in ...search.php on line 55"
By Zver

27th Aug 2009 1:39

I'm a beginner when it comes to using php. How exactly would I add urls to the addurl.php? Like could you give me an example of how to type it out?
By Raven

27th Aug 2009 19:52

Raven - When you access the "addurl.php" page, you'll see a form, where you just type in a web address. ( e.g; http://www.dalehay.com ) ... and that will add that page into the database for you.
By Dale Hay

1st Sep 2009 14:08

that was actually pretty good tutorial. thnx for that.
By Zeeshan

30th Nov 2009 16:25

mysql_fetch_array(): supplied argument is not a valid MySQ
By sssssssssss

2nd Dec 2009 0:55

I'm just getting my feet wet in PHP and found your search engine tutorial to be very good and informative, thanks!
By Ken

24th Dec 2009 3:46

got same error as Zver ans sssssss

mysql_fetch_array(): supplied argument is not a valid MySQL result resource..
By ee

10th Jan 2010 19:48

Bad script , sql injection
By Sakher

3rd Mar 2010 22:32

Thanks for sharing your knowledge.

Below the comment Note is threadened not good for visiters.
By

31st Mar 2010 21:08

great tutorial. i have an issue with a search engine i`m building t a new site (which is small for now): how can i search multiple tables that are not connected within themselves? i mean: i want the search to check for the string on every field in the DB that stores text. best regards
By Php Programmer

27th Apr 2010 19:55

For single word search, all is ok. What about multiple word search engine.
I have a situation where the input text (for search) composed of two or more words. In this case I have to explode the string into words and then search for each word in the database, for this I need to use nested loops (which i did) and I am getting duplicate results and is directly proportional to the number of words in the input text. What I should do now to remove the duplication in results.?
By Chitrali

19th Jun 2010 22:55

I am having the same problem that Chitrali is having. I have a large database of items. The search does not respond well with queries of multiple words. How can this be fixed?
By salvage man

10th Jul 2010 17:10

You missed something important for displaying information about the link.

Under
echo "<h2><a href='$ser[pageurl]'>$ser[pageurl]</a></h2>";

add
echo $ser[pagecontent];

Thats only if you add links thru a sql software such as phpmyadmin
By

11th Jul 2010 15:06

this php tutorial search engine is very help full and clean
By isu

30th Jul 2010 0:17

i am trying to use your code but it contain some errors
By recipesmela

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
:-( makes
:-D makes
:wtf: makes
;-) makes
:mad: makes
:omg: makes
:haha: makes

Your Comment:

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!

Latest News Stories
» Pectus Excavatum - Catchup
» Pectus Excavatum - The Final Chapter
» Pectus Excavatum Operation
» One Dream That Won't Be Fulfilled...
» Hotpoint WIXXE127 on Twitter Trends
» Google StreetView: Superheroes found in Blackpool
Top Viewed Stories
» PHP Mini Message Board Tutorial (40,846)
» Bypass Photobucket (32,474)
» PHP Search Engine Tutorial (29,360)
» Getting .htaccess on AppServ (Windows) to work (23,648)
» Rare McDonalds Monopoly 2008 Tickets (11,948)
» Helen Willetts Pregnant? (9,558)
Most Commented Stories
» PHP Mini Message Board Tutorial (101)
» PHP Search Engine Tutorial (37)
» Create your own Image Uploader (23)
» Pectus Excavatum (20)
» I like my DuMP (15)
» New Shameless Trailer! (10)