phpbeginner Posted September 8, 2010 Share Posted September 8, 2010 I am trying to get sql result based on a specific subpage id and also using the like. The subpage id does not seem to work and have tried to different codes below......any help would be appreciated. $sqlItem = "select * from tblsubpage WHERE subpageid='5' || title like \"%".$searchfor."%\" || paragraph like \"%".$searchfor."%\""; and also tried $sqlItem = "select * from tblsubpage WHERE subpageid='5' AND title like \"%".$searchfor."%\" || paragraph like \"%".$searchfor."%\""; Quote Link to comment https://forums.phpfreaks.com/topic/212872-where-and-like-help/ Share on other sites More sharing options...
AbraCadaver Posted September 8, 2010 Share Posted September 8, 2010 You need and to make sure the subpageid is always 5 and then use parentheses to group the OR expressions: $sqlItem = "SELECT * from tblsubpage WHERE subpageid='5' AND (title LIKE '%$searchfor%' OR paragraph LIKE '%$searchfor%')"; Quote Link to comment https://forums.phpfreaks.com/topic/212872-where-and-like-help/#findComment-1108753 Share on other sites More sharing options...
Psycho Posted September 8, 2010 Share Posted September 8, 2010 Database queries are interpreted similar to mathmatical expressions. Operations are interpreted from left to right by precedence! That means there is an order to which certain interpretations are made. In math multiplication/divisions are done before addition/subtraction. However, you can override that logic per your needs using parenthesis. Example: 5 + 2 * 10 = 5 + 20 (multiplication done first) = 25 (5 + 2) * 10 = 7 * 10 (addition done first) = 70 Two completely different results with the same numbers and operations because of the parenthesis. Database queries are the same. I forget all of the order of operations for query operations, but I do know that ANDs and ORs are of equal precedence. So, they are interpreted from left to right, so you need to ensure you parenthesis them as needed. As the query is interpreted from left to right the parser decides if the current comparison and all the preceeding are TRUE or FALSE before it moves to the next condition. For example, let's say you wanted to query for all the boys who play football or basketball. You might think this query would work: WHERE gender='male' AND sport='football' OR sport='basketball' But that would find all boys who play football and ALL poeple that play basketball. When an AND is encountered the previous result must be true and the current comparison must be true. When an OR is encounterd the previous must be true or the current comparisson must be true. So, if a record does not match the gender 'male' or the sport 'football' the OR makes all of that inconsequential if the record matches the sport 'basketball'. The correct query would look like WHERE gender='male' AND (sport='football' OR sport='basketball') So the record would have to be male AND the record would have to have one of the sports 'football' OR 'basketball' Quote Link to comment https://forums.phpfreaks.com/topic/212872-where-and-like-help/#findComment-1108762 Share on other sites More sharing options...
phpbeginner Posted September 8, 2010 Author Share Posted September 8, 2010 Very well explained and working fine! I thank you very much for your time. Quote Link to comment https://forums.phpfreaks.com/topic/212872-where-and-like-help/#findComment-1108764 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.