1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Simple Site Search

By

4 of 5

Breaking the PHP Code Down - Part 1
 if ($searching =="yes")
In our 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 we check before we run the query is that they actually entered a search string. If they haven't, we prompt them to, and don't process any more of the code. If we didn't have this code, and they entered a blank result, it would simply return the entire database's contents.

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

 $find = strtoupper($find)
This changes all of the characters of the search string to UPPER case. We will explain how this is useful later.
 $find = strip_tags($find)
This takes out any code they may have tried to enter in to the search box.
 $find = trim ($find)
And this takes out all the whitespace - for example if they accidently put a few spaces at the end of their query.
Related Video
Basic PHP Syntax
  1. About.com
  2. Technology
  3. PHP / MySQL
  4. PHP with MySQL
  5. Search your website with PHP and MySQL

©2014 About.com. All rights reserved.