BoarderLine Posted February 11, 2010 Share Posted February 11, 2010 I am using the following query as a DB search for a key word ($trimm). The other variables passed to the query are a category (the first %s which defaults to % if value doesn't exist) and City the second (the second %s which also defaults to % if value doesn't exist). The query is nearly working fine however if a match is found for the variable $trimm, and values exist in the query for the category and City, I am needing the result to be filtered to match all variables. As the query is at present if a match is achieved for $trimm it disregards the need for category and City to also match the hit. I hope this makes some sort sense to someone. Any help really appreciated. Ta. SELECT categories.Cat, cities.City, attrac.AttracID, attrac.AttracName, attrac.Add, attrac.Sub, attrac.List, attrac.Default, SUBSTRING(attrac_discrip.Discrip,1,350) AS Discription FROM (((attrac LEFT JOIN categories ON categories.CatID=attrac.List) LEFT JOIN cities ON cities.CityID=attrac.City) RIGHT JOIN attrac_discrip ON attract_discrip.AttracID=attrac.AttracID) WHERE attrac.List LIKE %s AND attrac.City LIKE %s AND attrac.Status='1' AND categories.Cat LIKE '$trimm' OR attrac.AttracID LIKE '$trimm' OR MATCH(attrac.AttracName) AGAINST ('$trimm') OR MATCH(attrac_discrip.Discrip) AGAINST ('$trimm')) ORDER BY attrac.AttracName Quote Link to comment https://forums.phpfreaks.com/topic/191744-search-query-help-please/ Share on other sites More sharing options...
BoarderLine Posted February 11, 2010 Author Share Posted February 11, 2010 Some additional info. Both Cat and City Variables are numeric. $cat_rsAttrac = "%"; if (isset($_GET['Cat'])) { $cat_rsAttrac = (get_magic_quotes_gpc()) ? $_GET['Cat'] : addslashes($_GET['Cat']); } $city_rsAttrac = "%"; if (isset($_GET['City'])) { $city_rsAttrac = (get_magic_quotes_gpc()) ? $_GET['City'] : addslashes($_GET['City']); } mysql_select_db($database); $query_rsAttrac = sprintf("SELECT categories.Cat, cities.City, attrac.AttracID, attrac.AttracName, attrac.Add, attrac.Sub, attrac.List, attrac.Default, SUBSTRING(attrac_discrip.Discrip,1,350) AS Discription FROM (((attrac LEFT JOIN categories ON categories.CatID=attrac.List) LEFT JOIN cities ON cities.CityID=attrac.City) RIGHT JOIN attrac_discrip ON attract_discrip.AttracID=attrac.AttracID) WHERE attrac.List LIKE %s AND attrac.City LIKE %s AND attrac.Status='1' AND categories.Cat LIKE '$trimm' OR attrac.AttracID LIKE '$trimm' OR MATCH(attrac.AttracName) AGAINST ('$trimm') OR MATCH(attrac_discrip.Discrip) AGAINST ('$trimm')) ORDER BY attrac.AttracName", GetSQLValueString($cat_rsAttrac, "text"),GetSQLValueString($city_rsAttrac, "text")); $query_limit_rsAttrac = sprintf("%s LIMIT %d, %d", $query_rsAttrac, $startRow_rsAttrac, $maxRows_rsAttrac); $rsAttrac = mysql_query($query_limit_rsAttrac) or die(mysql_error()); $row_rsAttrac = mysql_fetch_assoc($rsAttrac); Quote Link to comment https://forums.phpfreaks.com/topic/191744-search-query-help-please/#findComment-1010648 Share on other sites More sharing options...
kickstart Posted February 11, 2010 Share Posted February 11, 2010 Hi Problem appears to be operator procedence. Basically MySQL evaluates the ANDs before the ORs. Add some brackets to force the ORs to be evaluated first. SELECT categories.Cat, cities.City, attrac.AttracID, attrac.AttracName, attrac.Add, attrac.Sub, attrac.List, attrac.Default, SUBSTRING(attrac_discrip.Discrip,1,350) AS Discription FROM (((attrac LEFT JOIN categories ON categories.CatID=attrac.List) LEFT JOIN cities ON cities.CityID=attrac.City) RIGHT JOIN attrac_discrip ON attract_discrip.AttracID=attrac.AttracID) WHERE attrac.List LIKE %s AND attrac.City LIKE %s AND attrac.Status='1' AND ( categories.Cat LIKE '$trimm' OR attrac.AttracID LIKE '$trimm' OR MATCH(attrac.AttracName) AGAINST ('$trimm') OR MATCH(attrac_discrip.Discrip) AGAINST ('$trimm')) ORDER BY attrac.AttracName All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/191744-search-query-help-please/#findComment-1010655 Share on other sites More sharing options...
BoarderLine Posted February 11, 2010 Author Share Posted February 11, 2010 You Keith my friend are a legend. Thank you very much that worked a treat! :-) Quote Link to comment https://forums.phpfreaks.com/topic/191744-search-query-help-please/#findComment-1010659 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.