Jump to content

mySQL search


npsari

Recommended Posts

Hello. I have a table which is called [info], I store my members information there. Inside this table, there is a row called [interests] which contains all the members’ interests.

 

I am creating a search tool & I wish MySQL to match as many keywords as possible.

 

So if the user searches for: LOVE TO GO OUT AND HAVE COFFEE mySQL will display the member who has as many keywords of this search term as possible

 

These quires search for exact terms only…

 

$q = "SELECT * FROM info WHERE  interests LIKE '%{$search_term}%' ORDER BY RAND() ";

$q = "SELECT * FROM info WHERE  interests  =  ‘$search_term' ORDER BY RAND() ";

 

 

I need a MySQL query which will match as many keywords as possible and order the results according to most matching. Is this possible? Or is it a complicated story? Help please.

 

Link to comment
Share on other sites

That could get pretty complicated.

 

I suppose if you exploded the search terms into each keyword, and also query each user, then have them all in an array and display the highest number of results first......it's possible.

But that would be pretty intensive usage to do that for all your users.

 

I would suggest looking into mysql fulltext search in boolean mode, and read about the minimum word length, creating indexes

http://dev.mysql.com/doc/refman/5.6/en/fulltext-search.html

 

The following query will display a result if it contains at least one or more of the keywords in the search term.

 

$q = "SELECT * FROM info WHERE MATCH (interests) AGAINST ('$search_term' IN BOOLEAN MODE) ORDER BY RAND() ";

Link to comment
Share on other sites

Thank you so much.

Yesssss, this is so simple, I knew it exists somewhere

 

$q = "SELECT * FROM info WHERE
MATCH ( interests ) AGAINST ('$search_term')";
$res = @mysql_query($q);
while($r = @mysql_fetch_array($res)) {
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.