Noah Smith
Noah Smith

Reputation: 1

Add Multiple Term Search to my current query

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" );

Upvotes: 1

Views: 74

Answers (1)

isimmons
isimmons

Reputation: 2026

Possible solution I have not tested. submit post keywords as an array or collect them separately into an array.

Then either convert $keyword array to a string first with implode

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

and use

w.word_word IN ($keywords)

Or put the implode into your statement.

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

Either way you end up with mysql reading

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

refrences: Select from mysql table WHERE field='$array'?

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

Upvotes: 1

Related Questions