简单的SQL搜索调整

时间:2013-04-07 16:34:20

标签: php mysql search

我正在玩PHPFreaks“简单SQL搜索”脚本。我想知道如何在提交查询后优化搜索方法。

假设我提交了一个像“音乐”这样的搜索词,搜索查询会给我数据库中包含术语“音乐”的所有数据的结果,但是我想要搜索术语“音乐”一旦结果出现,我想改进搜索并查找“摇滚”或“说唱”类别中音乐下的所有结果。我想使用所选选项列表来完成此操作。

以下是来自PHPFreaks的工作脚本,我将其包含在内,因此如果需要,这里的用户也可以使用此脚本:

$error = array();
$results = array();


if (isset($_GET['search'])) {
   $searchTerms = trim($_GET['search']);
   $searchTerms = strip_tags($searchTerms); // remove any html/javascript.

   if (strlen($searchTerms) < 3) {
      $error[] = "Search terms must be longer than 3 characters.";
   }else {
      $searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection.
   }

   // If there are no errors, lets get the search going.
   if (count($error) < 1) {
      $searchSQL = "SELECT sid, sbody, stitle, sdescription FROM simple_search WHERE ";

      // grab the search types.
      $types = array();
      $types[] = isset($_GET['body'])?"`sbody` LIKE '%{$searchTermDB}%'":'';
      $types[] = isset($_GET['title'])?"`stitle` LIKE '%{$searchTermDB}%'":'';
      $types[] = isset($_GET['desc'])?"`sdescription` LIKE '%{$searchTermDB}%'":'';

      $types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)

      if (count($types) < 1)
         $types[] = "`sbody` LIKE '%{$searchTermDB}%'"; // use the body as a default search if none are checked

          $andOr = isset($_GET['matchall'])?'AND':'OR';
      $searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `stitle`"; // order by title.

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

      if (mysql_num_rows($searchResult) < 1) {
         $error[] = "The search term provided {$searchTerms} yielded no results.";
      }else {
         $results = array(); // the result array
         $i = 1;
         while ($row = mysql_fetch_assoc($searchResult)) {
            $results[] = "{$i}: {$row['stitle']}<br />{$row['sdescription']}<br />{$row['sbody']}<br /><br />";
            $i++;
         }
      }
   }
}

function removeEmpty($var) {
   return (!empty($var)); 
}
?>

结果:

 <?php echo (count($error) > 0)?"The following had errors:<br /><span id=\"error\">" . implode("<br />", $error) . "</span><br /><br />":""; ?>
      <form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm">
         Search For: <input type="text" name="search" value="<?php echo isset($searchTerms)?htmlspecialchars($searchTerms):''; ?>" /><br />
         Search In:<br />
         Body: <input type="checkbox" name="body" value="on" <?php echo isset($_GET['body'])?"checked":''; ?> /> | 
         Title: <input type="checkbox" name="title" value="on" <?php echo isset($_GET['title'])?"checked":''; ?> /> | 
         Description: <input type="checkbox" name="desc" value="on" <?php echo isset($_GET['desc'])?"checked":''; ?> /><br />
                 Match All Selected Fields? <input type="checkbox" name="matchall" value="on" <?php echo isset($_GET['matchall'])?"checked":''; ?><br /><br />
         <input type="submit" name="submit" value="Search!" />
      </form>
      <?php echo (count($results) > 0)?"Your search term: {$searchTerms} returned:<br /><br />" . implode("", $results):""; ?>

我希望有人能带领我朝着正确的方向前进。我对PHP / MySQL有点熟悉,并且无处不在网上看,但无法找到使用此脚本优化搜索结果的方法。感谢。

1 个答案:

答案 0 :(得分:0)

您有两种方法可以优化搜索结果。

  1. 将搜索(生成的行集)保存/序列化到磁盘/会话,以便在用户向搜索参数添加任何内容时进行搜索。在这个szenario你需要确保,用户没有改变“基础”搜索参数。如果他这样做了,你可以先运行另一个基础搜索,然后从结果中提取你的精炼结果(特定类型的条目)。

  2. 将新的搜索参数添加到某个base-query-string中,让数据库进行精炼搜索。

  3. 在这两种情况下,您最终得到的结果都是精炼的。我会选择第二个选项,你不需要关心以前的搜索参数。您只需将“流派”添加到查询中即可完成。