views:

531

answers:

4

I have to get records from my MySQL DB where:

sentto = "$username"

OR

sentto = "everyone"

How would I put this in a MySQL query? I tried a few things, but they don't seem to be working:

mysql_query("SELECT * 
             FROM pmessages 
             WHERE status='unread' 
                 AND sentto='$username' || sentto='everyone'");

mysql_query("SELECT * 
             FROM pmessages 
             WHERE status='unread' 
             AND sentto='$username' 
                 AND sentto='everyone'");

I seem to be stumped, if anyone knows how to do this the right way please let me know. This is a new scenario for me. Thank you!

+2  A: 

Taking the detail from one of your comments into account - use the " OR " keyword and parentheses to make sure that the right conditions are combined.

SELECT * FROM pmessages WHERE
   status = 'unread'
    AND
  (sentto = ? OR sentto = 'everyone')

Your problem was never with the OR, though, it was actually the AND precedence and lack of parentheses. The very significant detail that you completely omitted from your question was the additional test for "status = unread".

Note the use of ? above - you should really, really use prepared statements whenever combining MySQL and PHP, i.e.:

$sql = "..." # as above
$sth = $db->prepare($sql);
$res = $sth->execute($username);
while ($row = $sth->fetchrow()) {
    ...
}

(or the mysqli equivalent)

Alnitak
A: 

The word OR is what you're looking for:

mysql_query("SELECT * FROM pmessages WHERE sentto='$username' OR sentto='everyone'");

Macros
Yeah, I used that but it is returning one more record than it should be. Here is my current string:mysql_query("SELECT * FROM pmessages WHERE status='unread' AND sentto='$username' OR sentto='everyone' ");
Chris B.
enclose the two 'sentto' tests in parentheses. currently it's treated as "(status = 'unread' AND sentto = ...') OR sentto = 'everyone'"
Alnitak
That worked! Please post an answer
Chris B.
I did, but you accepted the other one... :(
Alnitak
+4  A: 
SELECT * 
FROM pmmessages 
WHERE sentto = '$username' 
    OR sentto = 'everyone'

Edit Chris, based on your new query of:

SELECT * 
FROM pmessages 
WHERE status='unread' 
    AND sentto='$username' 
OR sentto='everyone'

You need to modify it so that your AND stands alone (it is conflicting with your OR).

Rewrite it to this

SELECT * 
FROM pmessages 
WHERE status='unread' 
    AND 
        (sentto='$username' 
            OR sentto='everyone' )
TheTXI
Yeah, I used that but it is returning one more record than it should be. Here is my current string: mysql_query("SELECT * FROM pmessages WHERE status='unread' AND sentto='$username' OR sentto='everyone' ");
Chris B.
Thank you, Good to know.
Chris B.
Chris, it may do you well to find some information on SQL and the logic behind evaluating Boolean Operators like AND, NOT, and OR. You will find that if you don't group them together properly, it will evaluate incorrectly and you won't get the results you are looking for.
TheTXI
A: 

is everyone an actual value or do you want to return all results?

if you want to return all results set up something like this

if (@sentto is null)
begin

     set @sendto='%'

end

mysql_query("SELECT * FROM pmessages WHERE sentto='$username'")
DForck42