refining search query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
zoe
Forum Commoner
Posts: 36
Joined: Sat Jun 11, 2005 11:51 am
Location: Glasgow

refining search query

Post by zoe »

This is probably a very simple problem, but i can't think what to search for to get an answer without just asking...here goes

I'm selecting entries from my database based on the user's entry in to a form field named 'businesstype'. My query goes somehing like this:

Code: Select all

$query = ("SELECT * FROM business_directory WHERE Type LIKE '%$businesstype%'");
My question is, how can I structure the query to select any business that contains the user's search term only as a distinct word in the 'Type' field?

For example, if the user enters 'car', the results should include 'car dealers', but not 'caravan sites'.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

untested

Code: Select all

SELECT * FROM business_directory WHERE Type REGEXP('\b{$businesstype}\b')
zoe
Forum Commoner
Posts: 36
Joined: Sat Jun 11, 2005 11:51 am
Location: Glasgow

Post by zoe »

thanx, i'll try that now. :D
zoe
Forum Commoner
Posts: 36
Joined: Sat Jun 11, 2005 11:51 am
Location: Glasgow

Post by zoe »

That's returning no results for anything. I'm a bit out of my depth here. Should probably start reading up on REGEX :?
Thanks
Post Reply