dlabacs Posted July 21, 2011 Share Posted July 21, 2011 Hi guys, I have my php/mysql SELECT code which doesn't return a result to what I have expected after execution of the code. $sql="SELECT *FROM advert WHERE ad_Type = '%{$service}%' AND ad_Street_No like '%{$location}%' OR ad_Street_Name like '%{$location}%' OR ad_Suburb like '%{$location}%' OR ad_Postcode like '%{$location}%' OR ad_State like '%{$location}%'"; The user will search on location and the type of service available using parameters like street name,suburb, postcode and/or state in one textfield and then select a type of service from a dropdown menu. So for example : "Select records from table 'advert' where type of service = 'e.g. hauling,digging,' located at e.g. Cardiff NSW". " something like that. I have 2 records right now on my database : 1. Service: Hauling, Location: Cardiff NSW 2. Service: Digging , Location: Cardiff NSW Now the problem is: you search for "Hauling" service in "Cardiff" , will return 2 records (#1 and #2). you search for "Digging" service in "Cardiff", will return 2 records (#1 and #2). you search for either of the services in " Cardiff NSW", will return 0 results. I got no idea on how to fix this. Please help me guys. Thank you in advance. Best regards Quote Link to comment https://forums.phpfreaks.com/topic/242543-need-help-mysql-where-clause-doesnt-work-for-me/ Share on other sites More sharing options...
Barand Posted July 21, 2011 Share Posted July 21, 2011 My usual tip when using AND and OR in the same clause is to use parentheses. eg A AND B OR C Do you mean (A AND B) OR C or do you mean A AND (B OR C) Quote Link to comment https://forums.phpfreaks.com/topic/242543-need-help-mysql-where-clause-doesnt-work-for-me/#findComment-1245620 Share on other sites More sharing options...
dlabacs Posted July 21, 2011 Author Share Posted July 21, 2011 thank for the tip sir, .. I will try that one right away. Hmp, Is there any wrong on my code sir?? Quote Link to comment https://forums.phpfreaks.com/topic/242543-need-help-mysql-where-clause-doesnt-work-for-me/#findComment-1245623 Share on other sites More sharing options...
Barand Posted July 21, 2011 Share Posted July 21, 2011 use either = '$service' or LIKE '%$service%' but not = '%$service%' Quote Link to comment https://forums.phpfreaks.com/topic/242543-need-help-mysql-where-clause-doesnt-work-for-me/#findComment-1245674 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.