petenaylor Posted March 13, 2011 Share Posted March 13, 2011 Hi all I have a database where business listing has a table value called live and a value of 1 if it is to be shown on the website. This SQL query works great: $getfeaturedlistings = mysql_query("SELECT * FROM `listings` WHERE live = 1 AND featured = 1 AND $trimmedsqlarea = 1 AND $trimmedcategory = 1 ORDER BY ID DESC"); However, in the search script I am using this query: $getfeaturedlistings = mysql_query("SELECT * FROM `listings` WHERE live = 1 AND featured = 1 AND description LIKE \"%$trimmed%\" OR title LIKE \"%$trimmed%\" OR address LIKE \"%$trimmed%\" OR website LIKE \"%$trimmed%\" OR email LIKE \"%$trimmed%\" OR phone LIKE \"%$trimmed%\" ORDER BY id ASC"); But this brings back listings that are not live? (Where live is "0" in the SQL) Please help! Many thanks Pete Quote Link to comment https://forums.phpfreaks.com/topic/230543-sql-showing-all-listings/ Share on other sites More sharing options...
Pikachu2000 Posted March 13, 2011 Share Posted March 13, 2011 You're having precedence issues. I suspect you want to retrieve records that meet both of the first 2 `=` conditions, AND any one or more of the LIKE conditions, correct? If so, you need to add some parentheses to force precedence of the operators. They also make it easier to read . . . "SELECT * FROM `listings` WHERE ( live = 1 AND featured = 1 ) AND (description LIKE \"%$trimmed%\" OR title LIKE \"%$trimmed%\" OR address LIKE \"%$trimmed%\" OR website LIKE \"%$trimmed%\" OR email LIKE \"%$trimmed%\" OR phone LIKE \"%$trimmed%\") ORDER BY id ASC" Quote Link to comment https://forums.phpfreaks.com/topic/230543-sql-showing-all-listings/#findComment-1187174 Share on other sites More sharing options...
Pikachu2000 Posted March 13, 2011 Share Posted March 13, 2011 Oh, and don't double post. Quote Link to comment https://forums.phpfreaks.com/topic/230543-sql-showing-all-listings/#findComment-1187180 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.