bwaxse Posted July 29, 2006 Share Posted July 29, 2006 Alright so im pretty new to php and sql - I know theres an easier way to do this:I have Class IDs set in the Users Table and I want to get the Class Names by referencing the Classes table.The if statements - If a user has a class entered in, look up that ID and set the $_SESSION[CLASS#] equal to that name.<?php if ($_SESSION['CLASS1'] != "0"){ $query2 = "SELECT sClassName FROM tbl_classes WHERE sClassID ='".$_SESSION['CLASS1']."'"; $result2 = mysql_query($query2) or die("Invalid query: " . mysql_error()); $row2 = mysql_fetch_assoc($result2); $_SESSION['CLASS1'] = $row2['sClassName'];} if ($_SESSION['CLASS2'] != "0"){ $query3 = "SELECT sClassName FROM tbl_classes WHERE sClassID ='".$_SESSION['CLASS2']."'"; $result3 = mysql_query($query3) or die("Invalid query: " . mysql_error()); $row3 = mysql_fetch_assoc($result3); $_SESSION['CLASS2'] = $row3['sClassName'];}X4 More times?> Quote Link to comment Share on other sites More sharing options...
fenway Posted July 29, 2006 Share Posted July 29, 2006 An IN clause would be most appropriate:[code] "SELECT sClassName FROM tbl_classes WHERE sClassID IN ('".$_SESSION['CLASS1']."','".$_SESSION['CLASS2']."','".$_SESSION['CLASS3']."') ";[/code] Quote Link to comment Share on other sites More sharing options...
bwaxse Posted July 30, 2006 Author Share Posted July 30, 2006 Thanks a bunch, I used:$sql = "SELECT * FROM tbl_classes WHERE sClassID = '".$_SESSION['CLASS1']."'||'".$_SESSION['CLASS2']."'||'".$_SESSION['CLASS3']."'||'".$_SESSION['CLASS4']."'||'".$_SESSION['CLASS5']."'||'".$_SESSION['CLASS6']."'";Is there a major difference or is one better than the other? Quote Link to comment Share on other sites More sharing options...
fenway Posted July 30, 2006 Share Posted July 30, 2006 Yes there is... the one you have shouldn't work! You have to reference the column name as well with ORs. I prefer using IN clauses because they make the intent of the query much clearer, and they can be optimized by the parser. 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.