Search page

I am wondering what is a most common way to make a search. For example the following is my query if some data were submited in a search form. Is this a common way or a bad way?


if(isset($_GET['category'])){
  $join=$join."INNER JOIN ON categories users.userCat=categories.categoryId ";
  $category=$_GET['category'];
  if($firstCriteria==true){
    $criteria=$criteria." AND categories.categoryName='$category' ";
  }else{
    $firstCriteria=true;
    $criteria="WHERE categories.categoryName='$category' ";
  } 
}

if(isset($_GET['offering1'])){
  $join=$join."INNER JOIN ON offering users.userId=offering.userId ";
  $offering=$_GET['offering1'];
  if($firstCriteria==true){
    $criteria=$criteria." AND offering.offeringId='$offering' ";
  }else{
    $firstCriteria=true;
    $criteria="WHERE offering.userId='$offering' ";
  } 
}

if(isset($_GET['offering2'])){
...
 
$query="SELECT * FROM users ".$join.$criteria";

Depending on what you are searching you might need to use LIKE instead of =. That would particularly apply if you are searching text fields that contain more than one word.

For example


WHERE fieldToSearch LIKE '%searchterm%'

The % means match ‘searchterm’ anywhere in the field, so it could be at the start, end or between other words or characters.