how do i have multiple WHERE clauses???

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
syntax24
Forum Newbie
Posts: 11
Joined: Tue Jun 03, 2008 3:25 pm

how do i have multiple WHERE clauses???

Post by syntax24 »

Heres what I have:

Code: Select all

$announcements = mysql_query("SELECT * FROM announcements WHERE ato = $auth[userGroup] && WHERE ato = '0'") or die(mysql_error());
Obviously thats wrong as we all know... but how do I do something like that... i tried just an && without the 2nd WHERe... but that only pulled data that had both ato = usergroup and ato=0 .... i want to pull results IF ato is EITHER the usergroup OR 0, which is Global... I tried || but that still only listed 1 result... i have 2 in the database... 1 which ato is the group and 1 where its 0... so if it all works right, both should show.

heres the code i'm using to echo the results found...

Code: Select all

while ($announce = mysql_fetch_assoc($announcements)) {
                        echo "<tr>
                          <td width='23%' bgcolor=\"#F4F4F4\"><div align=\"center\"><em>"; 
                          
                          if($announce['ato'] == "1") { echo "Admins"; }
                          if($announce['ato'] == "2") { echo "Teachers"; }
                          if($announce['ato'] == "3") { echo "Parents"; }
                          if($announce['ato'] == "4") { echo "Students"; }
                          if($announce['ato'] == "5") { echo "Office"; }
                          if($announce['ato'] == "0") { echo "Global"; }
                          
                          echo "</em></div></td>
                          <td width=\"77%\" bgcolor=\"#F4F4FF\"><div align=\"left\"><a href='/view-announcements.php?id=".$announce['id']."' style='color:#000; text-decoration:none;'>".$announce['subject']."</a></div></td>
                        </tr>";
                        }
let me know if thats good or not too...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: how do i have multiple WHERE clauses???

Post by John Cartwright »

Code: Select all

.. WHERE foo = 1 AND fee = 2
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: how do i have multiple WHERE clauses???

Post by Benjamin »

The syntax is below. That query won't work though because a table field can never be two values at once.

Code: Select all

 
SELECT
  *
FROM
  announcements
WHERE
  ato = $auth[userGroup]
  AND ato = '0';
 
syntax24
Forum Newbie
Posts: 11
Joined: Tue Jun 03, 2008 3:25 pm

Re: how do i have multiple WHERE clauses???

Post by syntax24 »

So I guess I didn't catch it... will this work or not? Am I going to have to somehow combine 2 different queries to pull the data I need into one area?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: how do i have multiple WHERE clauses???

Post by Benjamin »

Someone asked this question a few days ago. Please see viewtopic.php?f=2&t=83551 for the answer.
Post Reply