Jakez Posted June 21, 2009 Share Posted June 21, 2009 OK I have a search form on my page, I want to search the database for each word someone types in, for example instead of matching the exact phrase "song lyrics" I want it to search for fields containing "song" AND/OR "lyrics". I tried this query below but no luck: SELECT * FROM table WHERE field LIKE 'song' OR field LIKE 'lyrics' OR other_field LIKE 'song' OR other_field LIKE 'lyrics' What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/163083-how-come-this-like-clause-isnt-working-to-search-words/ Share on other sites More sharing options...
Ken2k7 Posted June 21, 2009 Share Posted June 21, 2009 SELECT * FROM table WHERE field IN ('song', 'lyrics') OR other_field IN ('song', 'lyrics'); Quote Link to comment https://forums.phpfreaks.com/topic/163083-how-come-this-like-clause-isnt-working-to-search-words/#findComment-860482 Share on other sites More sharing options...
corbin Posted June 21, 2009 Share Posted June 21, 2009 SELECT * FROM table WHERE field IN ('song', 'lyrics') OR other_field IN ('song', 'lyrics'); Uh.... You either didn't read his question or you don't know what IN does. With LIKE you have to use certain symbols. ? means 1 of any character, and % means any amount of any character... So you could do something like: SELECT * FROM blah WHERE field LIKE '%someword%'; Quote Link to comment https://forums.phpfreaks.com/topic/163083-how-come-this-like-clause-isnt-working-to-search-words/#findComment-860536 Share on other sites More sharing options...
Ken2k7 Posted June 21, 2009 Share Posted June 21, 2009 I assumed the field is either song or lyrics. Perhaps I misunderstood. Sorry. And corbin, ? isn't a LIKE symbol. I think you meant "_" (without quotes). Quote Link to comment https://forums.phpfreaks.com/topic/163083-how-come-this-like-clause-isnt-working-to-search-words/#findComment-860549 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.