HDFilmMaker2112 Posted May 16, 2011 Share Posted May 16, 2011 I'm looking to know the best way to process a search query that has multiple words in the search: elseif(isset($_GET['search'])){ $search=$_GET['search']; $keyword=explode(" ",$search); //database query } Should I do a while loop query the database for each keyword? Quote Link to comment https://forums.phpfreaks.com/topic/236530-search-system-multiple-keywords/ Share on other sites More sharing options...
trq Posted May 16, 2011 Share Posted May 16, 2011 Should I do a while loop query the database for each keyword? Definitely not. I would use a simple IN() statement in this case. Quote Link to comment https://forums.phpfreaks.com/topic/236530-search-system-multiple-keywords/#findComment-1215972 Share on other sites More sharing options...
HDFilmMaker2112 Posted May 16, 2011 Author Share Posted May 16, 2011 alright, just making sure I'm on the right track here: elseif(isset($_GET['search'])){ $search=$_GET['search']; $keyword=explode(" ",$search); $keywords=implode(",",$keyword); $sql10000="SELECT product_id FROM $tbl_name2 WHERE keyword IN($keywords)"; $result10000=mysql($sql10000); while($row10000=mysql_fetch_array($result10000)){ $product_id3=$row10000['product_id']; $sql15000="SELECT * FROM $tbl_name WHERE product_id=".$product_id3; mysql($sql15000); } } Quote Link to comment https://forums.phpfreaks.com/topic/236530-search-system-multiple-keywords/#findComment-1215977 Share on other sites More sharing options...
HDFilmMaker2112 Posted May 16, 2011 Author Share Posted May 16, 2011 The below is returning: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/zyquo/public_html/ghosthuntersportal.com/pages.php on line 39 Line 39 is, while($row10000=mysql_fetch_array($result10000)){ elseif(isset($_GET['q'])){ $search=$_GET['q']; $keyword=explode("+",$search); $keywords=implode(",",$keyword); $sql10000="SELECT product_id FROM $tbl_name2 WHERE keyword IN($keywords)"; $result10000=mysql_query($sql10000); while($row10000=mysql_fetch_array($result10000)){ $product_id3=$row10000['product_id']; $sql15000="SELECT * FROM $tbl_name WHERE product_id=".$product_id3; $result15000=mysql_query($sql15000); while($row15000=mysql_fetch_array($result15000)){ extract($row1500); $content=$product_id; } } } Quote Link to comment https://forums.phpfreaks.com/topic/236530-search-system-multiple-keywords/#findComment-1216018 Share on other sites More sharing options...
trq Posted May 16, 2011 Share Posted May 16, 2011 Each keyword is a string so it will need to be surrounded by quotes. $keywords= "'" . implode("','",$keyword) . "'"; It's always a good idea (for debugging) to echo your sql queries so you can actually see what they look like. eg; echo $sql10000; Quote Link to comment https://forums.phpfreaks.com/topic/236530-search-system-multiple-keywords/#findComment-1216205 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.