Jump to content

sql query help


bwaxse

Recommended Posts

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
?>
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.