1. Home
  2. Computing & Technology
  3. PHP / MySQL
photo of Angela Bradley
Angela's PHP / MySQL Blog

By Angela Bradley, About.com Guide to PHP / MySQL

Simple PHP Search

Friday June 2, 2006
You can find almost anything on the internet... but how? By searching of course! We all use search engines like Google or Yahoo to find what we need online. On a smaller scale, we often search for products or articles within a very specific site.

One way to have a simply search on your site, is to have all your data contained in a MySQL database. We then use PHP to query the database, and return the results to the user.
Comments
July 4, 2006 at 9:37 pm
(1) seraaj says:

My CMS search function never worked, but I like the software. Have been trying for a long time to find a script tutorial that would help me understand what was happening within the code so I could finally get a search function on my site. THIS ROCKS! I still have some things to do with the script, but this script tutorial gave me a huge leg up!

Many thanks!!!

January 24, 2007 at 9:16 pm
(2) Jerry says:

I think what you’re trying to teach is interesting. Also, just to let you know that I’ve found something called MySearchParser at

http://www.addedworth.com/welcome/MySearchParser.php

which generates the search queries automatically. Also, it supports AND,
OR, NOT and Phrase searches. In short, it allows multiple keyword searches as opposed to single keyword searches.

Thanks for your tutorial. It was very helpful.

April 14, 2007 at 2:39 pm
(3) Joe says:

i fond error when install it.

Results

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/www/domain.info/index.php on line 39

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/www/domain.info/index.php on line 51
Sorry, but we can not find an entry to match your query

Searched For: example

November 1, 2007 at 9:00 pm
(4) Nihat says:

First of all, I would like to thank you.

I did, excatly you have showed in your page, but myone doesn’t work.

Please help.

Thanks

November 14, 2007 at 10:26 am
(5) anne says:

Hi,
I have also done exactly as it is shown in the tutorial and nothing shows up? no print no error messages nada…
help

November 18, 2007 at 3:28 pm
(6) Wayne says:

Hi,

I’m not getting any error messages, just a blank page after submtting the form, please can some one help. Is it MySQL database messedup? PHP. When I write scripts out of the php book without any forms I get results as soon as i use forms I get error messages unless the code is correct and then i get a blank page.

November 18, 2007 at 3:29 pm
(7) Waynr says:

Someone please e-mail me to offer help, e-mail address mustobaby@googlemail.com

thanks

February 8, 2008 at 1:40 am
(8) suresh says:

i used this code but i cant ale to get the results. at the same time i dont get any error too.. please me i need this….

February 15, 2008 at 10:04 am
(9) Chanuka says:

Your Coding gave me a big boost and motivation.It`s easy to understand and up to the level

thank you sooo much

Chanuka

February 20, 2008 at 12:52 pm
(10) boys says:

i hape try it your script. but when i click button Search it’s still blank :(
any 1 can help me

February 27, 2008 at 5:37 am
(11) Ozy says:

Hi,
i have done what you did in the tutorial but i do not get any results.(i only get blank page) i use wamp and save it as search.php in www directory. should i change my directory? Can you please help?

May 12, 2008 at 10:01 pm
(12) uk says:

It keep showing this warning message->
Warning: mysql_fetch_array():
I think it seems not working in actual search query =>$query = “select * from profile where upper($field) like ‘%$find%’ “;
Can anyone please leave the comment or solution for this??

May 28, 2008 at 5:28 pm
(13) Joshua says:

The problem most of you are having (blank page, no results) is caused by the fact that no where in the script is anything catching the POST data from the form and converting it into a variable. To correct this issue, simply insert this code at the very top of your PHP:

// Grab POST data sent from form
$field = @$_POST['field'] ;
$find = @$_POST['find'] ;
$searching = @$_POST['searching'] ;

Everything else in the script “should” work. :-)

May 30, 2008 at 11:24 pm
(14) Hampy says:

Still didnt work for me. I still get nothing at all. Any other suggestions?

June 2, 2008 at 1:17 pm
(15) Patrick says:

Joshua’s solution is right, but if you copy and paste it the symbols around field, find, and searching are not right. Try this one.

$field = $_POST['field'] ;
$find = $_POST['find'] ;
$searching = $_POST['searching'] ;

July 29, 2008 at 8:53 am
(16) Tony says:

Hi,

I’ve followed the script to the letter however when i submit the search the form returns itself but there isn’t any results or error messages…

I have checked out the comments and tried adding the fix above but still no joy.

I’m using xampp.

Please help q8-(

July 29, 2008 at 6:22 pm
(17) Mark says:

Im Not Experienced using mysql or php at all. im only beginning to learn it and i was just looking for tutorial for a search engine, i found this and used it but was wondering how to edit it. If someone is willing to help please reply.

July 30, 2008 at 4:12 pm
(18) Chris says:

I’m having the same problem as everyone else, Click submit and don’t get any results, and no error.

Even if I leave the field blank, and hit submit i don’t get the warning about not entering anything.

I’ve tried both fixes that were listed earlier, and that didn’t help either.

Does this code just not work?

Thanks

August 2, 2008 at 8:58 am
(19) Sakya Somnath Das says:

I’ve tried as you say but it doesn’t show anything nor any error or warning message. Please help

September 11, 2008 at 11:00 am
(20) Ev says:

I was experiencing the same problems as everyone else here with nothing happening to the script.

1)Change this line form.html
“>
to:

2) Makes sure the form is a html file not php!!eg form.html NOT form.php

3) Drop in this code at the very top of search.php underneath the

November 6, 2008 at 5:50 pm
(21) Paulina says:

Thanks for the code, and thanks to the comments that helped me.
A tip that would to help:
I change the line:action=”">
to
action=”">

And was successfull! :D

November 6, 2008 at 5:54 pm
(22) Paulina says:

Uhm… the code was broken… again then…

change in the form, line action PHP_SELF
to search.php

Hope will be usefull

December 3, 2008 at 5:17 pm
(23) Jon says:

I have tried all of the sugested fixes and nothing has worked. I key something in the search field, select an option, hit search and the page resets.

I have no idea what I’m doing wrong.

December 19, 2008 at 6:28 am
(24) j says:

As soon as I load the page, it comes up with the parse error.
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in

Then it’s funny because it will say “no matching results found” below that. So THAT part works;)

Now what it appears to not like is the $data in the line where you are passing mysql query to that variable. And then using that as an argument. Why it refers to that I don’t know though:(
For mine I changed the names as I’m using a different table than users but the concept is the same.

Online Recipe Search Results”;
}

?>

Any ideas on this ?
Otherwise great tutorial. I know more about searches anyways better than before I started to read it.

February 1, 2009 at 3:54 pm
(25) Caroline says:

Hey People, I think I know why this works from some people and not others (If I’m wrong please let me know, or I’ll bumbling about thinking I’m right on this one for a while :) )
This works perfect of my webhosts server and not my own testing server, think I have found the missing link, my host runs PHP 4.3.2 I run 5.2.8. A difference is that register_globals is turned off by default in the recent one and on in the older one. Now I think that this script relies on register globals being on, as it is a few years older and reasons mentioned earlier.
I’ve just started diggin on this one and from what I can gather the r_g (register_globals) setting allows variables and arrays to be passed through a script without being registered or defined, i.e. in the form above, without any $find=$POST_['find'], which would define the variable find from the form.
r_g was deprecated as it left open too many security issues etc see here
To find out if your r_g is on look in your info.php file, tutorial here.

Like I say I’m new to this so this could be wrong but I’ve got a hunch. Meanwhile I still havent found another simple search script!! I’m gonna mess around with this one, so ill write back if I get it sorted and share the love.

best wishes Caroline

February 1, 2009 at 4:16 pm
(26) caroline says:

Hey People,
I think I know why some people are having problems with this but if I’m wrong let me know or I’ll be bumbling about thinking Im right, doh!

My Proposal: The code relies on register globals being turned on, which is turned off by default since php version 4.2.0, to check if its on and which version your running check your info.php learn how here.

The reason I say this is because it works perfect on my site, hosted by server using php version 4.3.2, and their r_g (register_globals) is on and not on my testing server php 5.2.8, where it is off.
From what I can gather the r_g set to on allows variable that havent been defined as anything to be registered within your script, as in this one, so the form data i.e.name=”find” is recognised as $find with r_g on but not with it off.

To elaborate when r_g is turned off it doesn’t automatically become $find, it needs to have something like this $find=$_POST['find'] to make it register
and r_g turned on makes this magical transformation happen, and also our troublesome code.
Think it was changed to off due to security issues, read more.

I think this may also have something to do with people mentioning arrays, as there is also a setting for register_long_arrays. Then again this could be me (2+2=5)
So if I solve it I’ll share the love and post my solution, If this makes sense for anyone else, please post back so I know I’m on the right track.

best wishes Caroline

February 1, 2009 at 4:58 pm
(27) caroline says:

thanks for the tip Joshua/Patrick,
although I made another alteration, my script is now:

Results”;

etc Not forgetting to change method to get in the 1st lines,

thanks people
xxx caroline

February 3, 2009 at 3:53 am
(28) oops its caroline again says:

that should have been:

*input name=”search” type=”submit” value=”search”*
*/form*

*?php

$find = @$_GET['find'] ;

//If we submitted the form
if(isset($_GET['search']))
{
echo *”Results”*;

school boy error sorry, should have guessed this comment box would strip tags, I’ve replaced them here with *’s

This seemed to work fine for me, I was also wondering if the problems had anything to do with the registered globals being set to off in new versions of php. I say this because this script works fine when its on, and spits out error messages about defining variables when its not.
Just a hunch but if other people could test it with it on and off and maybe we could solve this bugger :) xx

February 4, 2009 at 2:34 am
(29) shea says:

your codes are very friendly..keep it up.. you helpinh us to are project…hehehehe thank you a lot… can ihave your email so that i can ask you more question…if thats ok with you..im shea from philippines….

February 26, 2009 at 9:03 am
(30) Rob says:

Actually, just to add to my previsou comment…
I am using cpanel to build my site and in the preview all of this works, but as soon as I publish it… poof nothing works. Please Help!

March 11, 2009 at 8:09 pm
(31) iz says:

hi, i want to create the form that can search the identity card(ic) number through the database, but i dont know how to connect the form to the data base, For example, if the customer want to check their request have been approved or not, they must fill their ic number to the form then they will click the button check. Then they will informed either their request have been approved or not.. can some one hel me about the coding.. Thank you..

April 24, 2009 at 8:24 am
(32) dany says:

you have to define $searching, $find and $field

to do so, right after “

May 9, 2009 at 8:23 pm
(33) Paul says:

Had the same problems as everyone else, and didn’t understand some of the comments above as they seemed to finish in mid sentence. I put the form in search.html and the php in search.php. In the html file I changed the action to be action=”search.php”. In search.php define the vars $field, $find and $searching; e.g. $field = $_POST["field"] before the test of $searching. Everything worked then.

May 18, 2009 at 3:37 pm
(34) nasty_nate says:

ok, i found the error

these 3 lines were not included, therefor the script would not know to get the values.

$field = $_REQUEST['field'];
$searching = $_REQUEST['searching'];
$find = $_REQUEST['find'];

June 24, 2009 at 6:16 pm
(35) Dan says:

Please help!
How can I use multiple search words using
explode function
thx.

August 6, 2009 at 7:48 am
(36) Rayudu says:

i am getting blank page…please help me in this regard….r else u can mark a mail to my mail id msrayudu1@gmail.com

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Discuss
Community Forum
Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL

©2009 About.com, a part of The New York Times Company.

All rights reserved.