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

大憨熊 提交于 2021-02-07 10:24:37

问题


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.


回答1:


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'"
}



回答2:


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);
}


来源:https://stackoverflow.com/questions/13312202/query-for-php-mysql-and-or-for-an-advanced-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!