Gazan Posted February 9, 2009 Share Posted February 9, 2009 Hey all. I'm stuck doing a mysql search function for my project.. What i have now, only returns results that matches 1 keyword in my form. What i want to, is to search for multiple keywords. I've been googling around, reading some tutorials, but i don't get the concept of explode() and storing the keywords into and array, and then do a query where you search for keywords that matches that array.. I need some explanation on how to do this. Current code: if(isset($_POST['keyword'])) { $keyword = $_POST['keyword']; if(empty($keyword)) { echo "You need to type a keyword to search for."; } else { $keyword = mysql_real_escape_string($keyword); $sql = "SELECT * FROM nyheder WHERE news_subject OR news_content LIKE '%".$keyword."%' ORDER BY news_date AND news_time"; $result_search_news = mysql_query($sql); $count_search_news = mysql_num_rows($result_search_news); if($count_search_news==0) { echo "You search for "<b>".$keyword."</b>" gave no results."; } else { echo "Your search for "<b>".$keyword."</b>" gave ".$count_search_news." results:"; echo "<br /><br />"; while($news_search = mysql_fetch_array($result_search_news)) { echo $news_search['news_subject']; echo "<br />"; } } } } Quote Link to comment Share on other sites More sharing options...
unkwntech Posted February 9, 2009 Share Posted February 9, 2009 Personaly assuming you don't need any advanced query options then just do a preg_replace('/ /', '%', $searchPhrase); This will replace all the spaces with the MySQL wildcard character, this is not the best way (or even nesicarily a good one) but it works well enough for phrases. Quote Link to comment Share on other sites More sharing options...
Gazan Posted February 9, 2009 Author Share Posted February 9, 2009 What would be a better way of doing it then? Anyhow, thanks alot. Quote Link to comment Share on other sites More sharing options...
unkwntech Posted February 9, 2009 Share Posted February 9, 2009 Theres an interesting little algorithm here and if that doesn't work try this Quote Link to comment 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.