Forum Moderators: coopster

Message Too Old, No Replies

Search for keywords in a single MySQL Column

         

txskydiver

9:05 pm on Feb 20, 2004 (gmt 0)

10+ Year Member



I am looking for a quick and easy way to take user entered keywords in a search field and search through a field in my MySQL table for matching words.

I will have a field in the database called "Keywords" and they will be separated by commas.

I know I can use:
$search_array = explode(" ",$searchwords);

to parse their submitted search terms into individual words, and

$keywords_array = explode(",",$keywords);

to break out the database field into individual words as well. I just cant think of the easiest way to compare here. Am I on the right track or have I lost my mind?

eugeneb

10:15 pm on Feb 20, 2004 (gmt 0)

10+ Year Member



u can use fulltext search for this search engine ...

read more about this [mysql.com...]

so u'll need only

$keywords = str_replace(","," ",$keywords);

to prepare the search string

webadept

11:10 pm on Feb 20, 2004 (gmt 0)

10+ Year Member



First, if you are doing this for a live website, you might want to check out htDig, and possibly use that, or go over to SourceForge or Freshmeat.net and see what they have there. Creating even a simple search that is passable, isnt' that simple

But if all you are doing is trying to make a search, to see how well you would do, then here is a couple of tips on the method you have choosen.

Since the words in the database are going to be in a field, seperated by comas (?) you could use two methods of checking them in there. (Well, two that I know about with out grabing a book), The first is using LIKE and the second is useing REGEX
[mysql.com...]

Regex is very powerful, and slightly faster than like, depending on the statment you use.

There is probably a reason for having all those words in there (what are we using here a varchar 255? field), but I can't think of a single reason I would do it that way.

There is a reason they might get like that eventually, but not one where they would start out like that.

Anyway, those are two methods which can get a word pattern out of a large text field.