mhgenterprises Posted August 22, 2007 Share Posted August 22, 2007 I have a form with one input where people can type in search terms. It then takes them to a script where it assigns a value to the search term and then runs it against a mysql product database. The problem is I would like to break the users search query into individual words for better search results. I have tried using explode() and it wont work. I have no idea why, this is the select statement I am using now including all the fields which need to be searched. Can you see any problems with it, maybe the problem isnt with the explode function but with my statement? If not, is there a simple alternative way to doing a search against a mysql db and breaking up a query into individual keywords? Thank you for the help. $rs = mysql_query("SELECT * FROM inventory WHERE condition = 'used' AND make LIKE '%" . $keywords . "%' OR type LIKE '%" . $keywords . "%' OR item_number LIKE '%" . $keywords . "%' OR year LIKE '%" . $keywords . "%' OR size LIKE '%" . $keywords . "%' OR finish LIKE '%" . $keywords . "%' OR model LIKE '%" . $keywords . "%' OR serial_number LIKE '%" . $keywords . "%' OR price LIKE '%" . $keywords . "%' OR information LIKE '%" . $keywords . "%' OR description LIKE '%" . $keywords . "%'"); Quote Link to comment https://forums.phpfreaks.com/topic/66193-problem-with-mysql-search-script/ Share on other sites More sharing options...
Fadion Posted August 22, 2007 Share Posted August 22, 2007 The query looks ok, but i guess it will be a stress of a query for mysql server, as it has smth like 10 ORs. Anyway a normal query should be like: $rs = mysql_query("SELECT * FROM inventory WHERE condition='used' AND make LIKE '%$keywords%'"); I just wrote it in one string without concatinating. About the explode thing: $words = explode(' ', $keywords); for($i = 0; $i < count($words); $i++){ if($i == 0){ $keyw = "'" . $words[$i] . "'"; } else{ $keyw .= " OR column='{$words[$i]}'"; } } $query = "SELECT * FROM table WHERE column LIKE " . $keyw; Didnt try it but guess it makes sense. It explodes the $keywords by ' ' (space) and then makes a variable $keyw which in the end will look like: "'word1' OR 'word2' OR 'word3'". About a better way to search u can try the Full-Text Indexing capabilities of mysql and theres a tutorial about that in phpfreaks tutorials. Quote Link to comment https://forums.phpfreaks.com/topic/66193-problem-with-mysql-search-script/#findComment-331094 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.