1

我正在尝试修改搜索路由,我有两个字段,艺术家和标题,当我搜索伪装者 Foo Fighters 时,我没有得到任何结果,而如果我单独搜索,我会得到。任何帮助将不胜感激谢谢。

  $arraySearch = explode(" ", $searchvalue);
  $arrayFields = array(0 => "Artist", 1 => "Title");
  $countSearch = count($arraySearch);
  $a = 0;
  $b = 0;
  $query = "SELECT * FROM ".$table." WHERE (";
  $countFields = count($arrayFields);
  while ($a < $countFields)
  {
    while ($b < $countSearch)
    {
      $query = $query."$arrayFields[$a] LIKE '%$arraySearch[$b]%'";
      $b++;
      if ($b < $countSearch)
      {
        $query = $query." AND ";
      }
    }
    $b = 0;
    $a++;
    if ($a < $countFields)
    {
      $query = $query.") OR (";
    }
  }
  $query = $query.")";
  $query_result = mysql_query($query);
  echo '<h1>Your Search Results</h1>'."\n\n";
  if(mysql_num_rows($query_result) < 1)
  {
    echo '<p>No matches found for "'.$searchvalue.'"</p>';
  }
  else
  {
    echo '<p>Search Results for "'.$searchvalue.'":</p>'."\n\n";
    while($row = mysql_fetch_assoc($query_result))
    {
      // output

    }
  }
4

3 回答 3

3

这个搜索功能非常低效。由于您正在搜索通配符%blah%,因此必须进行全表扫描才能找到任何记录。也就是说,如果您有 100 万条记录,它必须读取所有 100 万条记录才能找到您的搜索结果。在具有大表的繁忙站点上,这可能会破坏您的性能和数据库服务器。

如果您对表使用 MyISAM 引擎,我建议将FULLTEXT索引添加到艺术家和标题列,然后执行如下全文搜索:

SELECT * FROM `table` WHERE
MATCH(`artist`, `title`)
AGAINST('foo fighters the pretender');

正如 OP 所写,这是一个按搜索查询的匹配相关性排序的查询

$search = $db->escape('foo fighters the pretender');

$query = "SELECT *,
          MATCH(Artist, Title) AGAINST('$search' IN BOOLEAN MODE) AS relevance
          FROM table
          WHERE
            MATCH (Artist, Title)
            AGAINST('$search' IN BOOLEAN MODE)
          ORDER BY relevance DESC";
于 2012-07-25T00:07:19.947 回答
1

改变这个:

$query = $query." AND ";

对此:

$query = $query." OR ";

本质上你是在说SELECT... WHERE (field1 like 'foo%' AND field1 like 'bar%')。除非它们是完全相同的单词,否则您永远不会找到 field1 与两个单词相似的行。就像说SELECT ... WHERE id=1 AND id=2,不会拉任何结果,而SELECT ... WHERE id=1 OR id=2将拉第 1 行和第 2 行。

于 2012-07-25T00:00:19.920 回答
0

关闭查询的括号后,放置一个 var_dump($query) 以便您了解发生了什么。

例如:以下代码输出: string(123) "SELECT * FROM WHERE (Artist LIKE '%foo%' AND Artist LIKE '%fighters%') OR (Title LIKE '%foo%' AND Title LIKE '%fighters%' )"

<?php

$searchvalue = 'foo fighters';

$arraySearch = explode(" ", $searchvalue);
  $arrayFields = array(0 => "Artist", 1 => "Title");
  $countSearch = count($arraySearch);
  $a = 0;
  $b = 0;
  $query = "SELECT * FROM ".$table." WHERE (";
  $countFields = count($arrayFields);
  while ($a < $countFields)
  {
    while ($b < $countSearch)
    {
      $query = $query."$arrayFields[$a] LIKE '%$arraySearch[$b]%'";
      $b++;
      if ($b < $countSearch)
      {
        $query = $query." AND ";
      }
    }
    $b = 0;
    $a++;
    if ($a < $countFields)
    {
      $query = $query.") OR (";
    }
  }
  $query = $query.")";

  var_dump($query);
?>
于 2012-07-25T00:08:06.140 回答