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

Simple Site Search

By , About.com Guide

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.
Explore PHP / MySQL
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

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

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. PHP with MySQL
  5. php search script - php search engine - php site search

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

All rights reserved.