stephfox Posted March 17, 2007 Share Posted March 17, 2007 Hi, I need to SELECT records WHERE a string matches the content of a field... eg. I have field values like: Phone name Phone name pink Phone name blue Phone name polka-dot What I want is to have a MySQL query which selects ALL records which match the search term, eg. 'Phone name' returns all of the above, but not 'Phone': Currently my query looks like this: SELECT * FROM [table_name] WHERE [field_name] LIKE '%s' ORDER BY [value] ASC I'm sure this is easy, but can't get there myself.. Help! Any ideas? Thanks, S Link to comment https://forums.phpfreaks.com/topic/43153-solved-how-to-select-records-where-field-contains-a-particular-string/ Share on other sites More sharing options...
suzzane2020 Posted March 17, 2007 Share Posted March 17, 2007 Hi, You cud use "Full Text seraching" match("string") against(filedname1,fieldname2...) Link to comment https://forums.phpfreaks.com/topic/43153-solved-how-to-select-records-where-field-contains-a-particular-string/#findComment-209552 Share on other sites More sharing options...
Barand Posted March 17, 2007 Share Posted March 17, 2007 $search = "Phone name"; $sql = "SELECT * FROM `table_name` WHERE `value` LIKE '$search%' ORDER BY `value` ASC"; Link to comment https://forums.phpfreaks.com/topic/43153-solved-how-to-select-records-where-field-contains-a-particular-string/#findComment-209567 Share on other sites More sharing options...
stephfox Posted March 19, 2007 Author Share Posted March 19, 2007 Thanks for your help... I've solved this problem now, the error was that the query was being built into an sprintf() function, and then executed... I was trying to figure out what the '%s' part of the query was doing. Sprintf won't allow any % signs to be included in the query string as wildcards, and was causing the error... I've now built the query as a simple string variable, and now I can happily add in the % for the LIKE clause, and all works fine! Link to comment https://forums.phpfreaks.com/topic/43153-solved-how-to-select-records-where-field-contains-a-particular-string/#findComment-210534 Share on other sites More sharing options...
Barand Posted March 19, 2007 Share Posted March 19, 2007 Sprintf won't allow any % signs to be included in the query string as wildcards, Yes it will, just use "%%" when you want a "%" Link to comment https://forums.phpfreaks.com/topic/43153-solved-how-to-select-records-where-field-contains-a-particular-string/#findComment-210666 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.