Jump to content


Photo

sql query help


  • Please log in to reply
3 replies to this topic

#1 bwaxse

bwaxse
  • New Members
  • Pip
  • Newbie
  • 9 posts
  • LocationFort Worth Texas

Posted 29 July 2006 - 09:50 PM

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
?>
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." -AE

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 29 July 2006 - 09:51 PM

An IN clause would be most appropriate:

"SELECT sClassName FROM tbl_classes WHERE sClassID IN ('".$_SESSION['CLASS1']."','".$_SESSION['CLASS2']."','".$_SESSION['CLASS3']."') ";

Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 bwaxse

bwaxse
  • New Members
  • Pip
  • Newbie
  • 9 posts
  • LocationFort Worth Texas

Posted 30 July 2006 - 03:40 AM

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?
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." -AE

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 30 July 2006 - 04:33 PM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users