Spaceploit
Spaceploit

Reputation: 335

mysql string contained within a search string

What I need seems simple, but I haven't been able to pull it off so far. Maybe it's just these late hours, or maybe it's not that simple after all, I don't know any more :) So, here's the thing. I want to be able to check whether the search string from my site contains any of the fields from a particular column in my database. So, it would be the opposite of the usual one:

mysql_query("  
SELECT *  
FROM `table`  
WHERE `column` LIKE '%{$search}%'  
");

which looks for the fields with values where the search string is contained.
What would be the easiest way, using some regular expressions or...?

Thanks a bunch!

Upvotes: 2

Views: 1150

Answers (2)

Danosaure
Danosaure

Reputation: 3655

Just do it the other way around

SELECT *  
FROM `table`  
WHERE '{$search}' LIKE concat('%', `column`, '%')

bear with me for the proper syntax for variable escaping for SQL-injection.

Upvotes: 3

Chandresh M
Chandresh M

Reputation: 3828

your query is also good as you want. but still you can try this.

You can search your string by using MATCH() AGAINST() statement in mysql.

mysql_query("SELECT * FROM table_name WHERE MATCH(field11,field12) AGAINST ('searchstring')"); 


Here your field must be having Fulltext datatype.

This may work good.

Thanks.

Upvotes: 0

Related Questions