简体   繁体   中英

Query for PHP/MySql AND/OR for an advanced search

I'm new to php and mysql so I need some help with my query. This is my sql query

SELECT * FROM table1 WHERE (Name LIKE '%$keyword%' OR ZipCode LIKE '%$keyword%') AND Category LIKE '$category' AND Country LIKE '$country' LIMIT $start, $limit

Now what I want to do exaclty is

  1. This query should search column names with Name or ZipCode using a text field with the name keyword. That is either keyword matches the Name column or the ZipCode.
  2. The query should also work if no keyword is entered and only a country is selected from a dropdown.
  3. The query should also work if no keyword or country is input and only a category is selected from a dropdown.

In more simple words if either of the things keyword, country or category is selected the search should display results accordingly. Moreover it should also work if all the fields are input. I managed to do half of it but sometimes it gives me wrong results. Please help.

You can check which of the fields are set and make query accordingly at runtime. something like this you can do:

$query = "";
$keyword = $_REQUEST['keyword'];
$country = $_REQUEST['country'];
$category = $_REQUEST['category'];
if(isset($keyword)){//if keyword set goes here
   $query = "SELECT * FROM table1 WHERE Name LIKE '%$keyword%' OR ZipCode LIKE '%$keyword%' OR country LIKE '%$keyword%'";
   if(isset($category)){
     $query .= "AND category LIKE '$category'";
   }
   if(isset($country)){
     $query . = "AND country LIKE '$country'"
   }
}else if (isset($category)){ //if keyword not set but category set then goes here
  $query = "SELECT * FROM table1 WHERE category LIKE '$category'";
  if(isset($country)){
    $query . = "AND country LIKE '$country'";
  }
}else if(isset($country)){//if only country set goes here
  $query = "SELECT * FROM table1 WHERE country LIKE '$country'"
}

I would suggest using several different queries (one for each combination of search parameters), using php to either decide which to use or dynamically build one with php.

Note that using a like starting with a % is likely to be quite slow.

If you really wanted to do it as one statement (and I don't recommend it) then:-

SELECT * 
FROM table1 
WHERE (Name LIKE '%$keyword%' OR ZipCode LIKE '%$keyword%' OR '$keyword' = '') 
AND (Category = '$category' OR '$category' = '' )
AND (Country = '$country' OR '$country' = '' )
LIMIT $start, $limit

Or to dynamically build it up

$ConditionArray = array();
if ($keyword != '') $ConditionArray[] = "(Name LIKE '%$keyword%' OR ZipCode LIKE '%$keyword%' OR '$keyword' = '')";
if ($category != '') $ConditionArray[] = "Category = '$category'";
if ($country != '') $ConditionArray[] = "Country = '$country'";

if (count($ConditionArray) > 0)
{
    $query = "
    SELECT *
    FROM table 1
    WHERE ".implode(' AND ', $ConditionArray);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM