By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,996 Members | 892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,996 IT Pros & Developers. It's quick & easy.

Advanced Search Query

P: 4
Hello i will be a web designer one day but until then i need a little help

I have a search query that searches the database for user entered string. See below:

$query = "SELECT * From MyTable WHERE Company_Name LIKE \"%$postrimmedcompname%\" ORDER BY Company_Name" ;

This works fine. If I enter "estate agents", I get the results. and also if I enter "estate" or I enter "agents" I also get the results.

However, if I enter "estate agents housing", it doesnt display the results.

I thought using the LIKE operator and the % sign would make this work. But it doesnt.

if any out their could point me in the right direction i would be most grateful :)
May 16 '07 #1
Share this Question
Share on Google+
2 Replies


Atli
Expert 5K+
P: 5,058
The %..% signs tell you SQL to search for the string in between the signs anywhere inside the field value. So if you were to have a VARCHAR field containing the name 'John Doe' and you were to search for the value '%John%', it would return the field. Also, if you searched for '%John Doe%' it would return it. But if you go outside the value of the field, e.g. '%Hi John Doe%', the search string would not exist inside the field, ergo it would not return the field.

And to further complicate the matters, if you put a % sign at either end of the search string (e.g. 'John%'), but not the other end, it will assume that the seach string is at the end of the field your are searhing. So using our field 'John Doe', if you were to seach for 'John%' it would return the field, but if you were to search for 'Doe%' it would not.
May 16 '07 #2

Motoma
Expert 2.5K+
P: 3,235
You will need to do an explode on your string first, using " " as a delimiter. Then build your query with LIKEs combined with ORs.
May 16 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.