Tutorial: How to Create/Make your own search engine using PHP and MySql as Database without algorithm - For your Website

In this tutorial, You will learn "How to build your own search function" for allowing your visitors to search through your site via a Html search form or Button. Here, we are using php language and MySql database services to develop this functionality and is recommended to clear the basic concepts about both before reading more. I unexpectedly developed this when I'm helping one of my colleague and friend in completing his college assignment, So, I will provided you here with the most basic code you can use to do this task. You can also do this task in other languages but in that also you need to know the raw (most basic) sql queries and how to implement them in a particular language.

I suppose you know the basics of Structered query language and are already using them in your codes that were written for your site. So here's the first HTML code to create a search button and form to accept user input and query.

Paste the above code anywhere in your page within the body tag and can customize it's look via CSS. Now, here is the snippet of php code that will retrieve results from MySql database and I'm sure you never assumed this to be so easy. Save this code on another page with name "search.php" in the same directory or you can do changes as per your requirement. Here,

$_GET['query'] fetches or decodes the data provided via form from any webpage using 'GET' method as a parameter value of url or webpage address,

<?php
// Change the fields below as per the requirements
$db_host="Host Name";
$db_username="Username used to access database";
$db_password="Password to access database";
$db_name="Database u created for site";
$db_tb_name="Name of the table u created in site database";
$db_tb_atr_name="attribute(column name) of table in which search action is to be performed";
//Now we are going to write a script that will do search task
// leave the below fields as it is except while loop, which will display results on screen
mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");
$query=mysql_real_escape_string($_GET['query']);
$query_for_result=mysql_query("SELECT * FROM $db_tb_name WHERE
$db_tb_atr_name like '%".$query."%'");
echo "<h2>Search Results</h2><ol>";
while($data_fetch=mysql_fetch_array($query_for_result))
{
echo "<li>";
echo substr($data_fetch[$db_tb_atr_name], 0,160);
echo "</li><hr/>";
}
echo "</ol>";
mysql_close();
?>

You can also perform search task on multiple columns of table in this way

$query_for_result=mysql_query("
SELECT * FROM table_name WHERE
Column_1 like '%".$query."%'
OR
Column_2 like '%".$query."%'
");

Basics of Code snippet doing the search task on database you created for your website:

We are storing the changeable values in variables defined at the top of script,

Then, we are connecting and selecting the database we created for our site,

Then, we are executing the mysql query using mysql_query() function on a particular column name (attribute) of table in our site database. Note: We are using the '%' sign on both side of '$query' variable in 'WHERE' condition of mysql query to find the matching queried word in a sentence or in between a big word stored in our database.

Then, we are running 'while loop' condition to fetch all the rows containing the giving word till the last check and we are printing them on screen simultaneously using echo command.

Then, at last after the work is completed, we are closing the connection with database for the stability and security purposes.

Finally, the conclusion is "Script presented here contains nothing but an extra '%' and 'LIKE' commands in query which does the work of searching as according to the needs of most basic search engine with only one algorithm".

To Make it Work on Single Page (Section Added on 15:30 IST 22-09-2011)

If You think that creating two separate pages for both PHP Script and HTML form is little lengthier and frustrating task, then, here is the solution of it. You can copy and paste code shown below in a single page and it does not require extra modifications, other than those explained above. You can still make it work via other pages after setting the action attribute of HTML form pointing to this page.

You can Clearly see in PHP script below, We are using the isset() function of PHP, Simultaneously with "IF" PHP statement to check submission of form. If form is submitted, then, PHP Code will execute or vice versa.

Comments

No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.

sending

no idea

3 years ago

Hello,

I am confused on how to use or create a pHp database.

Can you please explain more about this?:

$db_host="Host Name";

$db_username="Username used to access database";

$db_password="Password to access database";

$db_name="Database u created for site";

$db_tb_name="Name of the table u created in site database";

$db_tb_atr_name="attribute(column name) of table in which search action is to be performed";

Can you show me some examples?

Thanx

AUTHOR

Krishna

3 years agofrom Dausa, India

Hi Prox,

Did you created a table in db, with your preferred column name and have data there before executing search query?

Regards,

Krishna

Prox

3 years ago

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/xampp/Searchengine.php on line 32

ronald

5 years ago

Thanks!

Jammer

6 years ago

can i put a link on the result? that can view their files.

s.pandey

6 years ago

nice one

Ryan Husband

6 years ago

how can i get this to use the outputted data as the url example www.example.com/$db_tb_atr_name

Ryan Husband

6 years ago

how can i get the data stored in $db_tb_atr_name to be used in hyperlink

Alex

6 years ago

Thank you very much for this tutorial, it was so useful ..

ucando262

6 years ago

This is great and easy to get working, however it is only 1/2 the battle. How can I extract a record number from the found results so I can update that particular record? The "A href" example above would be a great help if I could reference a found record for the user to edit... Now as it, it pulls all the records, but the user can't do anything with one of them... Could you provide one example of that? Thanks...

miniaudios

6 years ago

It's a really great and helpful article..

Thanks mate.

miniaudios

6 years ago

Friends, I as well need some help.

The response gives error as :

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/miniaudi/public_html/searchvideo/searchvideo.php on line 21

My DB is not empty..is there any specific I need to check

TDP

6 years ago

getting this error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in(my information)

// leave the below fields as it is except while loop, which will display results on screen

Collins

6 years ago

i figured out the problem, the table needed extra information. but now im getting this : Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /homepages/27/d394293414/htdocs/itservices.php on line 164

Line 164 : while($data_fetch=mysql_fetch_array($query_for_result))

AUTHOR

Krishna

6 years agofrom Dausa, India

@Collins, This error indicates that there is no such table exist in your database whose name is Question.

Check!

1) if database name is correct,

2) if table named as "Question" exist in your database on which you're performing your search.

Try performing search on another table of your database and check if response remains the same.

Collins

6 years ago

hey @itech my databse isn't empty i put data in it to test it, i just put this in my code to test it ... if ( ! $query_for_result ) {

die ( mysql_error() ); // terminate script and output error

} and i got this error Table 'db394335150.Question' doesn't exist but im not sure where im supposed to insert the change at, i followed this pages php to the T

AUTHOR

Krishna

6 years agofrom Dausa, India

Hello @Collis, Your database is empty.

Collins

6 years ago

I keep getting this error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /homepages/27/d394293414/htdocs/search.php on line 22

Line 22 ... while($data_fetch=mysql_fetch_array($query_for_result))

Please Help!

Joe

6 years ago

Amazing. Thank you!!

rain

6 years ago

it works ...thanks

jake

6 years ago

thank you very much for this code, i was able to apply it to my small database that i created for my company. i really like it. thank you thank you

Zquery

6 years ago

Great Tutorial for begginer...

Bruce Chamoff

6 years agofrom New York

Has anyone created a PHP search engine using Drupal or Joomla?

Marian

6 years ago

Thank you very much! This realy helped me! :) You are the best!

Bruce Chamoff

6 years agofrom New York

Hey Itech, I commented on this hub 7 months ago and see that you changed the PHP code to mysql_real_escape_string() to prevent SQL injections. Thanks. Nice to know someone took my advice. LOL Still, this was a great article.

AUTHOR

Krishna

6 years agofrom Dausa, India

Use contact form from my profile... I'll try my best to assist you.

Priya

6 years ago

can u please tell me how to create database in microsoft acess. i am creating a search engine in java

ddsoftware

6 years ago

I used this tutorial and it works perfectly.

But I need help.

Question: How do I attach the results found a url from the database because when give click on a result to be redictionat the website.

Thanks for help.

Logesh

6 years ago

Thank u so much.......

AUTHOR

Krishna

6 years agofrom Dausa, India

You're welcome.

jasmeen

6 years ago

thanks............a........ .lot.........

AUTHOR

Krishna

6 years agofrom Dausa, India

Check if column name of database table specified in MySQL query is not empty.

rajiv

6 years ago

arning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\TELEKOM project\search.php on line 22

danny

6 years ago

hi can any one make a search with 2 textfiled like the indeed.co.uk

thanks

AUTHOR

Krishna

6 years agofrom Dausa, India

Developing a searching engine like google/ask/yahoo is very hard task. The search engine we created here on this page is like a micro bacteria in respect to them. I would suggest not to waste your time in doing that because It requires lots of web space, time, good algorithms, multiple computers and Huge power of CPUs etc.

Well, If you want to start with small such as with single website or group of websites, then, you can do that by simply using MySQL and PHP. you just only need to manipulate and match strings.

Xafyr

6 years ago

hey everyone!

I want to create a search engine website like google/ask/yahoo... what should i do?? should i create my sql database? please help me...

coolaust

7 years ago

Great work very helpful,

I'm trying to alter this so it outputs a paginated list of photos with text one ofter another when you search, like some real estate sites list search results, can you help me understand the changes required?

najwa

7 years ago

correction.

*it is from "customer info" to "customer".

najwa

7 years ago

ok thanks. it's setel when i change my table name from "customer" to "customer info". it error when it have spacing.

thanks again.

AUTHOR

Krishna

7 years agofrom Dausa, India

One More Point : Does Column Name of Table on Which search action is to be performed has some rows containing data or is Blank. If Blank, Then, You need not to worry about this type of errors.

Thanks for Notifying Me.

AUTHOR

Krishna

7 years agofrom Dausa, India

It is working on my Computer... (Executed Again)

Check points listed below:

1) Did You Started MySQL before running this script on your server.

2) Did You Created Database, Table and it's attribute inside MySQL on which search action is to be performed.

3) Did You Submitted correct values to this variables,

$db_host="Host Name";

$db_username="Username used to access database";

$db_password="Password to access database";

$db_name="Database u created for site";

$db_tb_name="Name of the table u created in site database";

$db_tb_atr_name="attribute(column name) of table in which search action is to be performed";

OR You May Did some erroneous changes in MySQL Query.

najwa

7 years ago

why there are still have an error on line 22?

it give this command -->

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\TELEKOM project\search_result.php on line 22

AUTHOR

Krishna

7 years agofrom Dausa, India

Hello @petermourra, Thanks for notification.... I Updated this script again... Try it Now... It is working on my computer.

Note: Create a separate Page (search.php) for PHP Script as per the instructions shown above (If you're a newbie) Otherwise, You know how to deal with forms.

petermourra

7 years ago

i get a blank page when i run the search script.

AUTHOR

Krishna

7 years agofrom Dausa, India

@monika, I Also think so... But this article is for beginners...

If you want... I can write another tutorial on building advanced search engine while Using PHP as programming language.

My Qoute: "Nothing is impossible in programming - As long as you have interest and your concepts are clear - Basics are more important"

monika

7 years ago

bakwas

NTWALI Jean Paul

7 years ago

I appreciate this code, it's really very interesting to me because it turns my black day to yellow day.

My thanks go to hotwebideas who plays a great job on securing the code.

Warm regards!!

Bruce Chamoff

7 years agofrom New York

I think this is a great article, but you need to add some security from SQL injections. I would suggest that this article change the first line to $query=mysql_real_escape_string($_GET['query']);

Also, this line should be moved to the line under the MySQL database connection (line 17) to avoid an error.

Ishraga M Allam

7 years ago

The table name in the query of search is wrong. It has to be $db_tb_name and not $tb_name. Otherwise it is fine.

Thank you so much.

ram

7 years ago

if it would be explained that how to arrange and how we can make a good search engine in html ........

what is function of result.php..............

and how it works

how it assign value of desire thing in path in web site

themastudios

7 years agofrom Melbourne, FL

Very nice work, keep it up!

awe

7 years ago

heal!

function addi(a,b){

alert( a + b);

}

temp_inizer

8 years agofrom Noida (India)

Thanks...Its interesting, you provide such a wonderfull information, I'll try to do it.

This website uses cookies

As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

This is used to display charts and graphs on articles and the author center. (Privacy Policy)

Google AdSense Host API

This service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)

This is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)

Facebook Login

You can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)

Maven

This supports the Maven widget and search functionality. (Privacy Policy)

We may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.

Conversion Tracking Pixels

We may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.

Statistics

Author Google Analytics

This is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)

Comscore

ComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)

Amazon Tracking Pixel

Some articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)