如何在搜索引擎上检测精确搜索

时间:2012-09-27 16:45:30

标签: php mysql

在我的下面的脚本中,如果行类似于用户输入的行,则用户输入表单并从MYSQL表返回行。我正在建立一个搜索引擎,一切都是基于排名。但我希望能够调整下面的代码,看看“iPad”这个词有多少次出现了行字段,即“标题”,“描述”,“关键字”和“链接”。如果是这样,我希望该行返回高于具有更高ID的行,但仅在所有字段组合中提及一次iPad。 我的代码如下:

条款一起查询:

     $query = " SELECT * FROM scan WHERE "; 


$terms = array_map('mysql_real_escape_string', $terms);
$i = 0; 
foreach ($terms as $each) {
      if ($i++ !== 0){
            $query .= " AND "; 
      }
      $query .= "title LIKE '%{$each}%' OR link LIKE '%{$each}%' OR  keywords LIKE '%{$each}%' OR description LIKE '%{$each}%' ";
}   $query = mysql_query($query) or die('MySQL Query Error: ' . mysql_error( $connect ));
echo '<p class="time">Qlick showed your results in ' . number_format($secs,2) . ' seconds.</p>';     

 $numrows = mysql_num_rows($query);
if ($numrows > 0) {
      
      while ($row = mysql_fetch_assoc($query)) {
            $id = $row['id'];
            $title = $row['title'];
            $description = $row['description'];
            $keywords = $row['keywords'];
            $link = $row['link'];
            $rank = $row['rank'];

单独的条款查询

          $query = " SELECT * FROM scan WHERE "; 
    $terms = array_map('mysql_real_escape_string', $terms);
    $i = 0; 
    foreach ($terms as $each) {
          if ($i++ !== 0){
                $query .= " OR "; 
          }
          $query .= "title LIKE '%{$each}%' OR link LIKE '%{$each}%' OR  keywords LIKE '%{$each}%' OR description LIKE '%{$each}%' ";
      }
    // Don't append the ORDER BY until after the loop
            $query = mysql_query($query) or die('MySQL Query Error: ' . mysql_error( $connect ));
    $numrows = mysql_num_rows($query);
    if ($numrows > 0) {
            
          while ($row = mysql_fetch_assoc($query)) {
                $id = $row['id'];
                $title = $row['title'];
                $description = $row['description'];
                $keywords = $row['keywords'];
                $link = $row['link'];
                $rank = $row['rank'];

1 个答案:

答案 0 :(得分:0)

KISS 保持简单愚蠢

为什么没有字段“等级”。

计算包含该字词的字段数,并将其保存在排名字段