1

I have a basic search engine. In one script, it populates a MYSQL table with words scanned in the html web page. Based on one word search, it ranks the results when the words appeared the most in the web page.

But I need to work on multiple term querying. Is there a way to search multiple terms from my query below?

     $keyword = addslashes( $_POST['keyword'] );
     $results = addslashes( $_POST['results'] );

      /* Execute the query that performs the actual search in the DB: */

         $result = mysql_query(" SELECT p.page_url AS url,
                       COUNT(*) AS occurrences 
                       FROM page p, word w, occurrence o
                       WHERE p.page_id = o.page_id AND
                       w.word_id = o.word_id AND
                       w.word_word = \"$keyword\"
                       GROUP BY p.page_id
                       ORDER BY occurrences DESC
                       LIMIT $results" );
4

1 回答 1

1

可能的解决方案我没有测试过。将帖子关键字作为数组提交或将它们单独收集到数组中。

然后要么首先使用 implode 将 $keyword 数组转换为字符串

$keywords = implode(',', $_POST['keyword']);

并使用

w.word_word IN ($keywords)

或者将内爆放入您的声明中。

w.word_word IN (".implode(',', $keywords').")

无论哪种方式,您最终都会阅读 mysql

w.word_word IN (keyword1, keyword2, keyword3 ...)

参考: 从 mysql 表中选择 WHERE 字段='$array'?

http://data.agaric.com/mysql-where-1-4-5-syntax

于 2012-11-03T18:11:49.160 回答