Mysql SELECT LIKE must match whole words for variable only

I have a string variable $ and I use

 SELECT * FROM db WHERE description LIKE '%$string%' OR headline LIKE '%$string%'

As you can see, I want to find two fields "description" and "headline" to see if the string variable matches any of them.

The problem is that I want it to match whole words !!!

Example: if the description contains "hi", it is enough if $ string is "h". this is not what i want. It should correspond only to the whole word!

I split the query into words for this? or what?

+5
source share
2 answers

, FULLTEXT. , MyISAM:

CREATE TABLE test (
  id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  headline VARCHAR(120) NOT NULL,
  description VARCHAR(255) NOT NULL,
  FULLTEXT(headline, description)
) ENGINE=MyISAM;

:

SELECT *
FROM test
WHERE MATCH (headline,description) AGAINST('$string');

.

+9

, , REGEXP.

:

SELECT *
  FROM db
 WHERE description REGEXP '[[:<:]]$string[[:>:]]' = 1
    OR headline REGEXP '[[:<:]]$string[[:>:]]' = 1

. http://dev.mysql.com/doc/refman/5.1/en/regexp.html

+13

All Articles