views:

61

answers:

1

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";
+8  A: 

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 
codaddict
It was strange that I still get no result after putting a bracket before I post this question.
proyb3
Ok, I manage to get it right use !empty() to validate each parameter and add them into mysql statement if there a value.
proyb3
Yeah, thank you, but I didn't have an account. I experienced in mysql, some time too much code make my brain go haywire.
proyb3