Instructions to Create a Simple Search Form With PHP Script

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.

of 05

The HTML Search Form

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.

of 05

The PHP Search Code

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.

of 05

Breaking the PHP Code Down - Part 1

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.

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.

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

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

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

of 05

Breaking the PHP Code Down - Part 2

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.

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.

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.

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.

mla apa chicago
Your Citation
Bradley, Angela. "Instructions to Create a Simple Search Form With PHP Script." ThoughtCo, Jan. 29, 2020, Bradley, Angela. (2020, January 29). Instructions to Create a Simple Search Form With PHP Script. Retrieved from Bradley, Angela. "Instructions to Create a Simple Search Form With PHP Script." ThoughtCo. (accessed March 24, 2023).