Forum Moderators: coopster

Message Too Old, No Replies

Search the SQL table based on entered search words

I know of three ways to do this, any more?

         

H2O_aa

4:06 pm on Jun 22, 2005 (gmt 0)

10+ Year Member



1. use LIKE and wildcard:
.....where field1 LIKE "%$searchword%";

2. use regular expression:
.....where field1 REGEXP '[[:<:]]$searchword[[:>:]]'";

3. use mysql's full-text searching

Are there any other methods? I would like to make sure that I know all the possible options so I could choose one that works best in my case.

onematchfire

7:43 pm on Jun 22, 2005 (gmt 0)

10+ Year Member



Depending on how demanding the search is, if you really needed, you could make a dictionary table of each word found in each entry (via a nightly cron or the like) and use the dictionary table to map each words to which entries it is found in

mcibor

7:44 pm on Jun 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's a method to work around similar by content.

Log each search word, or those which didn't return any output, into a db
and then manually put the word that should be searched as well.

CREATE TABLE searches(searched VARCHAR(50), tosearch VARCHAR(50));
The query would look sth like this:


$sql = "IF((SELECT searched FROM searches WHERE searched='$searchword') IS NULL) THEN (INSERT INTO searches(searched) VALUES('$searchword'))";
//I'm not really sure about this, but I wanted to add to a database a word, that is not already there.
//query the db

//here the search query
$sql = "(SELECT field1 FROM table WHERE field1 LIKE '%$searchword%') UNION (SELECT table.field1 FROM table, searches WHERE field1 LIKE '%searches.tosearch%' AND searches.searched='$searchword')"

This shall do
Best regards
Michal Cibor