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

Simple Site Search


3 of 5

The Search Code
 //This is only displayed if they have submitted the form 
 if ($searching =="yes") 
 echo "<h2>Results</h2><p>"; 
 //If they did not enter a search term we give them an error 
 if ($find == "") 
 echo "<p>You forgot to enter a search term"; 
 // Otherwise we connect to our 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 we display the results 
 while($result = mysql_fetch_array( $data )) 
 echo $result['fname']; 
 echo " "; 
 echo $result['lname']; 
 echo "<br>"; 
 echo $result['info']; 
 echo "<br>"; 
 echo "<br>"; 
 //This counts the number or results - and if there wasn't any it gives them a little message explaining that 
 if ($anymatches == 0) 
 echo "Sorry, but we can not find an entry to match your query<br><br>"; 
 //And we remind them what they searched for 
 echo "<b>Searched For:</b> " .$find; 
This is the code that actually does the searching, and can be placed either above or below the HTML form in the file depending on your preference. Although it is explained in the comments, we will explain more about how this works on the next pages.
Related Video
Basic PHP Syntax
  1. About.com
  2. Technology
  3. PHP / MySQL
  4. PHP with MySQL
  5. The Search Code - Simple Site Search in PHP / MySQL

©2014 About.com. All rights reserved.