Chris James Champeau
Chris James Champeau

Reputation: 994

Mysql query ignoring NULL and empty fields

I have a very crazy query I know, but when I run it it returns all matches regardless of whether or not field mvideo is empty or not.

the query is:

SELECT * 
FROM `weaponsprojects` 
WHERE mvideo != "" 
  AND credit7 = "' .$person. '" 
   OR credit2 = "' .$person. '" 
   OR credit3 = "' .$person. '" 
   OR credit4 = "' .$person. '" 
   OR credit5 = "' .$person. '"
   OR credit6 = "' .$person. '"
   OR credit1 = "' .$person. '"
   OR credit8 = "' .$person. '"
   OR credit9 = "' .$person. '"
   OR credit10 = "' .$person. '"
   OR credit11 = "' .$person. '"
   OR credit12 = "' .$person. '"
   ORDER BY usort'

and it returns when any of the credits equal the person but it completely ignores the mvideo part. Why is this??

Edit: the field is not NULL, just simply empty no spaces just blank

Upvotes: 0

Views: 1391

Answers (1)

BigFatBaby
BigFatBaby

Reputation: 1510

i think that the WHERE condition is logically flawed... you need some precedence here, like so:

SELECT * 
FROM `weaponsprojects` 
WHERE mvideo != "" 
  AND (credit7 = "' .$person. '" 
   OR credit2 = "' .$person. '" 
   OR credit3 = "' .$person. '" 
   OR credit4 = "' .$person. '" 
   OR credit5 = "' .$person. '"
   OR credit6 = "' .$person. '"
   OR credit1 = "' .$person. '"
   OR credit8 = "' .$person. '"
   OR credit9 = "' .$person. '"
   OR credit10 = "' .$person. '"
   OR credit11 = "' .$person. '"
   OR credit12 = "' .$person. '")
   ORDER BY usort'

by adding the ( and the ) we tell the WHERE statement: "either mvideo is not empty and it matches one of the OR statements"

Upvotes: 4

Related Questions