2

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!

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

hmm, this makes sense, but it didnt work for me...no errors, just didnt filter the right ones out.
@spaceploitator: Hum.. would need to show table definition, few entries and expected results then.
the column field type is varchar in latin1_swedish_ci, with all the entries actually being 5 digit numbers. so i'd like to match a '12345' entry if i search for '123456'.
@spaceploitator: select '123456' like concat('%', '12345', '%'); yield true, so maybe the syntax/escape of '{$search}' isn't correct?
yes, you were right, i was doing something wrong. thanks a lot! im sorry i cant give you a 1up :)
0

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.

1 Comment

hi chandresh, yes, i was familiar with match against, but it wont work for me cos of the datatype. thanks anyway!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.