tags:

views:

75

answers:

5

Hi, I was hoping someone could give me a hand on how I write this correctly.

$details_quey = mysql_query("SELECT * FROM `accounts` WHERE ONLINE = 'true' OR FORCE_AWAY = 'true' OR AWAY = 'true' AND FORCE_OFFLINE = 'false' AND TOKEN = '$con_token' ", $CON )

Any help much appreciated.

A: 
"SELECT * FROM `accounts` WHERE (ONLINE = 'true' OR FORCE_AWAY = 'true' OR AWAY = 'true') AND FORCE_OFFLINE = 'false' AND TOKEN = '$con_token' ", 

Just use some braces to keep the and or logic seperate. Check if the person is online or force away or away AND that the forceoffline is false and token value = con token.

JonH
simply explained, bobs you uncle! Thanks
Phil Jackson
A: 

Not sure what you're looking for, but brackets sure will help for the OR's:

$details_quey = mysql_query("SELECT * FROM `accounts` WHERE (ONLINE = 'true' OR FORCE_AWAY = 'true' OR AWAY = 'true') AND FORCE_OFFLINE = 'false' AND TOKEN = '$con_token' ", $CON )
Kyle Rozendo
+1  A: 
SELECT * FROM `accounts` 
WHERE 
(ONLINE = 'true' OR FORCE_AWAY = 'true' OR AWAY = 'true')
AND FORCE_OFFLINE = 'false' AND TOKEN = '$con_token' 

Guessing a bit, but I think that is what you want.

Assuming you want anything matching any one of the ORs and all of the ANDs

Russell Steen
A: 

It's hard to tell what exactly you're trying to accomplish, but assume you want:

(online = true OR force_away=true OR away=true) AND force_offline=false AND token=$con_token

Your ORs need to be grouped together in parenthesis or the ANDs won't make much sense.

SELECT * FROM `accounts` WHERE (ONLINE = 'true' OR FORCE_AWAY = 'true' OR AWAY = 'true' AND FORCE_OFFLINE = 'false') AND TOKEN = '$con_token
David Lively
+1  A: 

AND has precedence over OR in a SQL Query. Therefore, your query valuates as such right now:

SELECT * FROM `accounts` WHERE
    ONLINE = 'true' OR
    FORCE_AWAY = 'true' OR
    ( AWAY = 'true' AND 
    FORCE_OFFLINE = 'false' AND
    TOKEN = '$con_token' );

Simply use ( ) to group conditions which should be evaluated together:

SELECT * FROM `accounts` WHERE
    (ONLINE = 'true' OR
    FORCE_AWAY = 'true') OR
    (AWAY = 'true' AND 
    FORCE_OFFLINE = 'false') AND
    TOKEN = '$con_token' );
Andrew Moore