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

Simple Site Search

By , About.com Guide

5 of 5

Breaking the PHP Code Down - Part 2

$data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'")
This code actually does the searching. We are choosing all the data from our table WHERE the 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 capitol P. We also use the '%' percentage on either side of our $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 our 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, it means that no results were found. If this is the case, we let the user know that.
$anymatches=mysql_num_rows($data)
Finally, incase they 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.

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
  4. PHP with MySQL
  5. php search script - php search engine - sql site search

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

All rights reserved.