PHP: searching a word in a string in mysql?

I am trying to find a word in a string in a mysql database using PHP.

The problem I am facing is that it searches and spits out the result on my PHP page, but any word close to the searched word also gets the result on my page.

to explain this better, let's say I have a line that looks like this:

women clothing

Now, if I search for a word that women, the results are displayed on my page correctly. But if I search for a word men, then the same results are displayed on my page, because the word "women" has the word men in it.

This is my current SQL query:

SELECT id, category_name, image_url FROM data WHERE `category_name` LIKE '%$sub%' GROUP BY category_name

I tried changing '%$sub%'to '$sub', but this does not return any results!

can anyone consult on this?

Edit:

category_name , , API.

. , , - , .. ..

, . , .

, "" "", .

+4
4

MySQL REGEXP, .

SELECT ... WHERE column REGEXP '[[:<:]]$sub[[:>:]]' ...
+3

, , , , , , . , woMEN , MEN .

, , . , : LIKE '% $sub %' , MEN, MEN . .

, , : LIKE $sub% ( ), , MEN . , , MENACE. , @mrun, - $sub %. , .

EDIT:

, , , , , MENS , , , MYSQL , ..

+1

. :

SELECT id, category_name, image_url FROM data WHERE `category_name` LIKE '% $sub %' OR `category_name` LIKE '% $sub' OR `category_name` LIKE '$sub' OR `category_name` LIKE '$sub %' OR `category_name` LIKE '% $sub\' %' OR `category_name` LIKE '% $sub' OR `category_name` LIKE '$sub\'' OR `category_name` LIKE '$sub\' %' GROUP BY category_name

- MATCH AGAINST, FULLTEXT, :

SELECT id, category_name, image_url FROM data WHERE MATCH (`category_name`) AGAINST ('+$sub' IN BOOLEAN MODE) GROUP BY category_name
+1

, , . :

SELECT id, category_name, aw_image_url FROM data WHERE category_name RLIKE '[[:<:]]".$sub."[[:>:]]' GROUP BY category_name
+1

Source: https://habr.com/ru/post/1615395/


All Articles