jeger003 Posted February 7, 2009 Share Posted February 7, 2009 i want to create a simple search that would just search for a few words. My problem is when i enter more than one word it would search for the exact phrase. I need it to search for any matching words.. dont have to be in order. i would appreciate some help. Link to comment https://forums.phpfreaks.com/topic/144174-issues-with-search-using-select-from-table-where-field-like-words/ Share on other sites More sharing options...
Lodius2000 Posted February 7, 2009 Share Posted February 7, 2009 <?php //from the searchform $search_words = "homer simpson peter griffin"; $search_words = explode($search_words, " "); foreach($search_words as $search_word){ mysql_query("SELECT * FROM table WHERE field LIKE $search_word") ?> I dont think that solves the problem completely but should get you moving in the right direction Link to comment https://forums.phpfreaks.com/topic/144174-issues-with-search-using-select-from-table-where-field-like-words/#findComment-756550 Share on other sites More sharing options...
jeger003 Posted February 7, 2009 Author Share Posted February 7, 2009 <?php //from the searchform $search_words = "homer simpson peter griffin"; $search_words = explode($search_words, " "); foreach($search_words as $search_word){ mysql_query("SELECT * FROM table WHERE field LIKE $search_word") ?> thanks for the help lodius2000 i used your code and played around with it but i am not able to get it to work. I either get an error saying the foreach() is not used correctly or i would get a blank screen this code below i get a blank screen <?php <?php include "db.php"; ini_set('error_reporting', E_ALL); //from the searchform $search_words = "used car for sale"; $search_words = explode($search_words, " "); foreach($search_words as $search_word) { $query = mysql_query("SELECT title, description, search_text FROM listings WHERE search_text LIKE '%$search_word%' OR title LIKE '%$search_word%'") or die(mysql_error()); while($result = mysql_fetch_array($query)) { echo $result; } } ?> Link to comment https://forums.phpfreaks.com/topic/144174-issues-with-search-using-select-from-table-where-field-like-words/#findComment-756764 Share on other sites More sharing options...
printf Posted February 7, 2009 Share Posted February 7, 2009 use a stop word filter to filiter words that should be ignored, that way you return only relevant results... Then after you fliter your words... use a regex to only return whole word matches... $search_words = "used car for sale"; $search_words = implode ( '|', array_map ( 'trim', explode ( ' ', $search_words ) ) ); $query = mysql_query("SELECT title, description, search_text FROM listings WHERE search_text REGEXP '[[:<:]]". $search_words . "[[:>:]]';") or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/144174-issues-with-search-using-select-from-table-where-field-like-words/#findComment-756772 Share on other sites More sharing options...
jeger003 Posted February 7, 2009 Author Share Posted February 7, 2009 use a stop word filter to filiter words that should be ignored, that way you return only relevant results... Then after you fliter your words... use a regex to only return whole word matches... $search_words = "used car for sale"; $search_words = implode ( '|', array_map ( 'trim', explode ( ' ', $search_words ) ) ); $query = mysql_query("SELECT title, description, search_text FROM listings WHERE search_text REGEXP '[[:<:]]". $search_words . "[[:>:]]';") or die(mysql_error()); for some reason i continue to get a a blank screenn i even have error_reporting on ini_set('error_reporting', E_ALL); $search_words = "used car for sale"; $search_words = implode ( '|', array_map ( 'trim', explode ( ' ', $search_words ) ) ); $query = mysql_query("SELECT title, description, search_text FROM listings WHERE search_text REGEXP '[[:<:]]". $search_words . "[[:>:]]';") or die(mysql_error()); while($row = mysql_fetch_array($query)) { echo $row['title']; } i removed everything having to do with REGEXP and it brought back results so i know its working Link to comment https://forums.phpfreaks.com/topic/144174-issues-with-search-using-select-from-table-where-field-like-words/#findComment-756800 Share on other sites More sharing options...
jeger003 Posted February 7, 2009 Author Share Posted February 7, 2009 use a stop word filter to filiter words that should be ignored, that way you return only relevant results... Then after you fliter your words... use a regex to only return whole word matches... $search_words = "used car for sale"; $search_words = implode ( '|', array_map ( 'trim', explode ( ' ', $search_words ) ) ); $query = mysql_query("SELECT title, description, search_text FROM listings WHERE search_text REGEXP '[[:<:]]". $search_words . "[[:>:]]';") or die(mysql_error()); WHOA! it works! i was made it search in titles instead of search_text. But one last question now. It possible it make it search in titles, description, and search_text?? could i just use OR like this?? WHERE search_text REGEXP '[[:<:]]". $search_words . "[[:>:]]' OR title REGEXP '[[:<:]]". $search_words . "[[:>:]]' OR description REGEXP '[[:<:]]". $search_words . "[[:>:]]'" or is there another way to do it? Link to comment https://forums.phpfreaks.com/topic/144174-issues-with-search-using-select-from-table-where-field-like-words/#findComment-756814 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.