Canman2005 Posted November 6, 2007 Share Posted November 6, 2007 Hi all I have a search form which has a field with the id of "keyword" I then have a query SELECT * FROM mydata WHERE `content` LIKE '%".$_GET['keyword']."%' This then returns results. Is it possible to split the value of the form field, then query each word separate? For example, if I enter "internet design" into the field and then run a query, it would query the words "internet" and "design" separate. So it basically splits the value and queries both words. Is this possible? Thanks in advance Dave Link to comment https://forums.phpfreaks.com/topic/76203-split-variable-then-query/ Share on other sites More sharing options...
sford999 Posted November 6, 2007 Share Posted November 6, 2007 I think this is what you're after: explode() Link to comment https://forums.phpfreaks.com/topic/76203-split-variable-then-query/#findComment-385715 Share on other sites More sharing options...
rajivgonsalves Posted November 6, 2007 Share Posted November 6, 2007 some food for thought $arrSearches = explode(" ",$_GET['keyword']); $arrMatches = array(); foreach ($arrSearches as $strKey => $strValue) { array_push($arrMatches," `content` like '%$strValue%' "); } `content` "SELECT * FROM mydata WHERE ".implode(" or ", $arrMatches); Link to comment https://forums.phpfreaks.com/topic/76203-split-variable-then-query/#findComment-385736 Share on other sites More sharing options...
Canman2005 Posted November 6, 2007 Author Share Posted November 6, 2007 Thanks rajivgonsalves I've been playing around with what you suggest and ended up with the following $arrSearches = explode(" ",$_GET['keyword']); $arrMatches = array(); foreach ($arrSearches as $strKey => $strValue) { array_push($arrMatches," `content` like '%$strValue%' "); } $sql = "SELECT * FROM mydata WHERE ".implode(" or ", $arrMatches)""; $query = @mysql_query($sql,$connection) or die(mysql_error()); while ($row = mysql_fetch_array($query)) { print $row['content']; } But im getting the error Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/npa/public_html/members/search/searchscript.php on line 10 Do you know where im going wrong? Sorry, im only good at simple queries (but learning) Thanks in advance Dave Link to comment https://forums.phpfreaks.com/topic/76203-split-variable-then-query/#findComment-385821 Share on other sites More sharing options...
rajivgonsalves Posted November 6, 2007 Share Posted November 6, 2007 should be this $sql = "SELECT * FROM mydata WHERE ".implode(" or ", $arrMatches)""; it should be $sql = "SELECT * FROM mydata WHERE ".implode(" or ", $arrMatches).""; notice the dot (.) Link to comment https://forums.phpfreaks.com/topic/76203-split-variable-then-query/#findComment-385826 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.