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...
- 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)
Feedback
I'd love to hear your feedback. If you have any then please contact
Dale Hay (that's me!) or leave a comment!
37 Comment(s) -:- Permalink
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 ;)
17th Sep 2008 18:16
really very good and simple By 3m masr
29th Sep 2008 5:35
nice tut!
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
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.
5th Nov 2008 6:26
:P thankss :D very much
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>";
}
?>
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. :)
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
7th Jun 2009 1:47
i've made a search code generator.
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.
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
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.
8th Aug 2009 15:22
"
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in
...search.php on line
55"
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.
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!
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
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.?
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?
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