devi: (Default)
devi ([personal profile] devi) wrote2005-01-30 05:19 pm

My brain! My brain!

Any SQL experts online tonight? Help!


I'm sorting out the school intranet. It's got an area for each subject, and teachers are supposed to be able to post messages in their own subject areas - except that messages have been showing up all over the place most of the time. Like, a sociology message I posted showed up in Biology. A message someone else posted in the Japanese area showed up everywhere.

I've found out why. The offending bit of code's here (from the AS messages file - there's an identical one for A2 and another for GCSE):

$sql = "SELECT * FROM subjectmessages WHERE subject='$_POST[subject]'and display='yes' and level='a' or level='gen' or level='as' order by id desc";

It's been parsing that statement as "select messages that a) have the right subject and the level A, or b) have level 'gen' (never mind the subject), or c) have level 'as' (and the same disregard of subject)".

What I can't figure out, though, is how to write an SQL query which does what I want it to - that is, 'choose all messages that have the right subject and any one of these three levels'.

Suggestions?

Edit: It works! Woo! Thanks, [livejournal.com profile] syleth, [livejournal.com profile] ruudboy and [livejournal.com profile] mooism for all giving the right answer with spooky speed...

[identity profile] ruudboy.livejournal.com 2005-01-30 05:28 pm (UTC)(link)
You need brackets round the 'or' bit, like:

$sql = "SELECT * FROM subjectmessages WHERE subject='$_POST[subject]'and display='yes' and (level='a' or level='gen' or level='as') order by id desc";