proyb3
proyb3

Reputation: 19

AND, OR output wrong result

Suppose I use 2 AND and one OR to retrieve a result, first test with input text value on name, I could get correct result but when I change $getc to any value other than empty string, the result does not change, it only query name value. What is wrong?

$query1 = "SELECT * FROM $tableName WHERE name LIKE '%$asd%' OR descriptions LIKE
    '%$asd%' AND category='$getc' AND company_type='$dsp' LIMIT $start, $limit";

Upvotes: 1

Views: 83

Answers (1)

codaddict
codaddict

Reputation: 455010

Try putting () around name LIKE '%$asd%' OR descriptions LIKE '%$asd%' as:

$query1 = "SELECT * FROM $tableName WHERE (name LIKE '%$asd%' OR descriptions LIKE '%$asd%') AND category='$getc' AND company_type='$dsp' LIMIT $start, $limit";

Which says category and company_type must match and at least one of name and descriptions must match.

Reason:

a OR b AND c AND d 

is treated as following as AND is having higher precedence than OR

a OR (b AND c AND d)

but what you want is:

(a OR b) AND c AND d 

Upvotes: 8

Related Questions