-1

如何使用以下代码搜索多个字段:

if (count($error) < 1) {

   $searchSQL = "SELECT sid, sdate, stitle, slocation, skategori, stype, sbody, slist FROM job_search WHERE ";

   # grab the search types.
   $types = array();
   $types[] = isset($_GET['title'])?"`stitle` LIKE '%{$searchTermDB}%'":'';
   $types[] = isset($_GET['desc'])?"`skategori` LIKE '%{$searchTermDB}%'":'';
   $types[] = isset($_GET['list'])?"`slist` LIKE '%{$searchTermDB}%'":'';          
        $types = array_filter($types, "removeEmpty"); 

   if (count($types) < 1) {

      $types[] = "`sbody` LIKE '%{$searchTermDB}%'"; 
      $andOr = isset($_GET['matchall'])?'AND':'OR';
      $searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `stitle`"; 
      $searchResult = mysql_query($searchSQL) or trigger_error("Error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");

   }
}

我在 中使用了一个复选框,$types = array();如果未选中任何复选框,则搜索将使用$types[] = "sbody LIKE '%{$searchTermDB}%'";.

我想知道的是如何$types[]在 中创建多个{$searchTermDB}并仅使用搜索所有字段 LIKE '%{$searchTermDB}%'";

4

2 回答 2

-1
SELECT sid, sdate, stitle, slocation, skategori, stype, sbody, slist 
FROM job_search 
WHERE MATCH(`stitle`, `skategori`, `slist`) 
AGAINST('{$searchTermDB}' IN BOOLEAN MODE) 

您可以构建与当前操作类似的字段字符串。以上是完整查询的基本示例。

请注意,您确实应该使用 PDO 或 mysqli 之类的东西来处理数据库通信。mysql_ 函数已弃用。

于 2013-04-22T17:04:26.347 回答
-1

这是解决方案。完全归功于@Kaiqing 的帮助。我放弃了 $types = array(); 并将其替换为以下内容:

  // If there are no errors, lets get the search going.
   if (count($error) < 1) {
      $searchSQL = "
      SELECT sid, sdate, stitle, slocation, skategori, stype, sbody, slist 
      FROM job_search 
      WHERE MATCH(`stitle`, `skategori`, `slist`, `sbody`, `slocation`) 
      AGAINST('{$searchTermDB}' IN BOOLEAN MODE) ";

      $searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was:        {$searchSQL}");

我没有使用 PDO,安全的选择是使用 PDO 方法。

于 2013-04-22T17:28:16.077 回答