Clinton Posted September 10, 2007 Share Posted September 10, 2007 Hey, I'm trying to run multiple WHERE statements in a single query but it's not working out. How would I do this? $q1=("SELECT * FROM roster WHERE (Position<='4' AND Terminated='0') ORDER BY Position"); Quote Link to comment Share on other sites More sharing options...
almightyegg Posted September 10, 2007 Share Posted September 10, 2007 $q1 = mysql_query("SELECT * FROM roster WHERE Position <= '4' AND Terminated = '0' ORDER BY Position") or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
Clinton Posted September 10, 2007 Author Share Posted September 10, 2007 Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Terminated = '0' ORDER BY Position' at line 1 <? // connect to database include("inc/connect.php"); // include auth and nav include("inc/auth.php"); include("inc/nav.php"); ?> <? $rn=("SELECT * FROM users WHERE username='" . $_SESSION['username']."'"); $rz=mysql_query($rn); while($row=mysql_fetch_array($rz)) { if($row["Status"]=="0"){ echo ("<br>Have a great day! <a href='logout.php'>Close</a>"); }else{ ?> <center><h3>Explosive Personnel</h3></center> <? $q1=mysql_query("SELECT * FROM roster WHERE Position <= '4' AND Terminated = '0' ORDER BY Position") or die(mysql_error()); $q1rz=mysql_query($q1); while($row1 = mysql_fetch_array($q1rz)){ ?> <b><font color="orange" size="3"><? echo $row1['Name']; ?></font></b><br> <i><? echo $row1['Title']; ?></i><br> Cell: <? echo $row1['CellPhone']; ?>       Home: <? echo $row1['HomePhone']; ?><br> E-Mail: <a href=mailto:<? echo $row1['Email']; ?>><? echo $row1['Email']; ?></a><br> <? echo $row1['Notes']; ?><p> <? } ?> </body> </html> <? }} ?> Quote Link to comment Share on other sites More sharing options...
almightyegg Posted September 10, 2007 Share Posted September 10, 2007 $q1 = "SELECT * FROM roster WHERE Position<='4',Terminated='0' ORDER BY Position" $q1rz=mysql_query($q1); Quote Link to comment Share on other sites More sharing options...
Clinton Posted September 10, 2007 Author Share Posted September 10, 2007 Still getting that same error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Terminated='0' ORDER BY Position' at line 1 Quote Link to comment Share on other sites More sharing options...
almightyegg Posted September 10, 2007 Share Posted September 10, 2007 hmm....try $q1rz = mysql_query("SELECT * FROM roster WHERE Position<='4',Terminated='0' ORDER BY Position ") or die(mysql_error()); $q1rz=mysql_query($q1); scrap that line Quote Link to comment Share on other sites More sharing options...
Clinton Posted September 10, 2007 Author Share Posted September 10, 2007 LoL. Wow. Same Error. I was seriously not expecting this to be that hard. :-\ Quote Link to comment Share on other sites More sharing options...
almightyegg Posted September 10, 2007 Share Posted September 10, 2007 for god's sake!! >.< lol $q1rz = mysql_query("SELECT * FROM roster WHERE Position=<'4',Terminated='0' ORDER BY Position ") or die(mysql_error()); I thought the <= was the right way round but better just check it by putting it =< Quote Link to comment Share on other sites More sharing options...
Clinton Posted September 10, 2007 Author Share Posted September 10, 2007 Yea, it just created more of an error when I reversed it. I know it has to do with the whole Termination thing for the simple fact that it was working, the query, before I tried to add that. Quote Link to comment Share on other sites More sharing options...
almightyegg Posted September 10, 2007 Share Posted September 10, 2007 $q1rz = mysql_query("SELECT * FROM roster WHERE Position=<'4' and Terminated='0' ORDER BY Position ") or die(mysql_error()); I replaced the and in lowercase in hope that the other chages I've made have made it need the and now I really help WHEREs now >.< Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 10, 2007 Share Posted September 10, 2007 Did you try: $q1= "SELECT * FROM roster WHERE Position<=4 AND Terminated=0 ORDER BY Position DESC"; Quote Link to comment Share on other sites More sharing options...
Clinton Posted September 10, 2007 Author Share Posted September 10, 2007 No I didn't but I just did and it didn't work either: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Terminated=0 ORDER BY Position DESC' at line 1 Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 10, 2007 Share Posted September 10, 2007 Do those columns actually exist, btw? What happens if you just do: $q1= "SELECT * FROM roster WHERE Terminated=0 ORDER BY Position DESC"; Quote Link to comment Share on other sites More sharing options...
almightyegg Posted September 10, 2007 Share Posted September 10, 2007 If Jesirose's suggestion doesn't work try this - $q1rz = mysql_query("SELECT * FROM roster WHERE `Position`=<'4' and `Terminated`='0' ORDER BY Position ") or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
Clinton Posted September 10, 2007 Author Share Posted September 10, 2007 Interesting. I never thought about doing that but when I do I get an error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Terminated=0 ORDER BY Position DESC' at line 1 But if I do it the other way, Position, it works just fine. And yes, i'm not a complete idiot. ;-) LoL THe columns do exist. I've checked and rechecked my PHPAdmin and everything looks good. Quote Link to comment Share on other sites More sharing options...
almightyegg Posted September 10, 2007 Share Posted September 10, 2007 it is case sensitive, maybe try without the capital at the beginning Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 10, 2007 Share Posted September 10, 2007 OH. Terminated is a keyword. You need to either rename it or use backticks `Terminated` Quote Link to comment Share on other sites More sharing options...
Clinton Posted September 10, 2007 Author Share Posted September 10, 2007 Holy crap it worked! SELECT * FROM roster WHERE `Position`=<'4' and `Terminated`='0' ORDER BY Position What's the purpose of all the those weird dashes? (And how did you make them? Can't find them on my keyboard.) Quote Link to comment Share on other sites More sharing options...
Clinton Posted September 10, 2007 Author Share Posted September 10, 2007 You know, I wondered if it might have been something like that. Thanks a ton. Quote Link to comment Share on other sites More sharing options...
almightyegg Posted September 10, 2007 Share Posted September 10, 2007 ....wow, something I suggested worked On my keyboard they are above tab key, press shift and it Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 10, 2007 Share Posted September 10, 2007 Whenever you get an error like that check the reserved words list: http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html They're backticks, next to the 1. Quote Link to comment Share on other sites More sharing options...
Clinton Posted September 10, 2007 Author Share Posted September 10, 2007 Got it. Thanks a million. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.