Simple Site Search

01
of 05

Creating the Database

Having a search feature on your site is handy for helping users find exactly what they are looking for. Search engines can range from simple to complicated.

This search engine tutorial assumes that all the data you want to be searchable is stored in your MySQL database. It does not have any fancy algorithms—just a simple like query, but it works for basic searching and gives you a jumping off point to make a more complex search system.

This tutorial requires a database. The code below creates a testing database to use as you work through the tutorial.

 CREATE TABLE users (fname VARCHAR(30), lname VARCHAR(30), info BLOB); 

 INSERT INTO users VALUES ( "Jim", "Jones", "In his spare time Jim enjoys biking, eating pizza, and classical music" ), ( "Peggy", "Smith", "Peggy is a water sports enthusiast who also enjoys making soap and selling cheese" ),( "Maggie", "Martin", "Maggie loves to cook itallian food including spagetti and pizza" ),( "Tex", "Moncom", "Tex is the owner and operator of The Pizza Palace, a local hang out joint" ) 
02
of 05

The HTML Search Form

 

Search

Seach for: in First NameLast NameProfile

This HTML code creates the form your users will use to search. It provides a space to enter what they are looking for, and a drop-down menu where they can choose a field they are searching (first name, last name or profile.) The form sends the data back to itself using the PHP_SELF () function. This code does not go inside the tags, but rather above or below them.

03
of 05

The PHP Search Code

 Results

"; //If the user did not enter a search term, they receive an error if ($find == "") { echo "

You forgot to enter a search term"; exit; } // Otherwise we connect to the database mysql_connect("mysql.yourhost.com", "user_name", "password") or die(mysql_error()); mysql_select_db("database_name") or die(mysql_error()); // We preform a bit of filtering $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); //Now we search for our search term, in the field the user specified $data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'"); //And display the results while($result = mysql_fetch_array( $data )) { echo $result['fname']; echo " "; echo $result['lname']; echo "
"; echo $result['info']; echo "
"; echo "
"; } //This counts the number or results. If there aren't any, it gives an explanation $anymatches=mysql_num_rows($data); if ($anymatches == 0) { echo "Sorry, but we can not find an entry to match your query

"; } //And reminds the user what they searched for echo "Searched For: " .$find; } ?>

This code can be placed either above or below the HTML form in the file depending on your preference. A breakdown of the code with explanations appears in the following sections.

04
of 05

Breaking the PHP Code Down - Part 1

 if ($searching =="yes")

In the original HTML form, we had a hidden field that sets this variable to "yes" when submitted. This line checks for that. If the form has been submitted, then it runs the PHP code; if not, it just ignores the rest of the coding.

 if ($find == "")

The next thing to check before running the query is that the user actually entered a search string. If they haven't, we prompt them to do so and don't process any more of the code. If we didn't have this code, and the user entered a blank result, it would return the entire database's contents.

After this check, we connect to the database, but before we can search, we need to filter.

 $find = strtoupper($find)

This changes all the characters of the search string to upper case.

 $find = strip_tags($find)

This takes out any code the user may have tried to enter in the search box.

 $find = trim ($find)​

And this takes out all the white space—for example, if the user accidently put a few spaces at the end of their query.

05
of 05

Breaking the PHP Code Down - Part 2

 $data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'")

This code does the actual searching. We are choosing all the data from our table WHERE the field they choose is LIKE their search string. We use upper () here to search the uppercase version of the fields. Earlier we converted our search term to uppercase as well. These two things together basically ignore case. Without this, a search for "pizza" would not return a profile that had the word "Pizza" with a capital P. We also use the '%' percentage on either side of the $find variable to indicate that we are not looking solely for that term but rather that term possibly contained in a body of text.

while($result = mysql_fetch_array( $data )) 

This line and the lines below it start a loop that will cycle through and return all the data. We then choose what information to ECHO back to the user and in what format.

 $anymatches=mysql_num_rows($data); 
 if ($anymatches == 0) 

This code counts the number of rows of results. If the number is 0, no results were found. If this is the case, we let the user know that.

 $anymatches=mysql_num_rows($data) 

Finally, in case the user forgot, we remind them of what they searched for.

If you anticipate a large number of query results, you may wish to use pagination to display your results.

Format
mla apa chicago
Your Citation
Bradley, Angela. "Simple Site Search." ThoughtCo, Aug. 21, 2017, thoughtco.com/simple-site-search-2694116. Bradley, Angela. (2017, August 21). Simple Site Search. Retrieved from https://www.thoughtco.com/simple-site-search-2694116 Bradley, Angela. "Simple Site Search." ThoughtCo. https://www.thoughtco.com/simple-site-search-2694116 (accessed November 19, 2017).