Jump to content

Recommended Posts

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."%\"";

Link to comment
https://forums.phpfreaks.com/topic/212872-where-and-like-help/
Share on other sites

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%')";

Link to comment
https://forums.phpfreaks.com/topic/212872-where-and-like-help/#findComment-1108753
Share on other sites

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'

Link to comment
https://forums.phpfreaks.com/topic/212872-where-and-like-help/#findComment-1108762
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.