Simple Beginner Question about SELECT WHERE

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PHPstarter
    New Member
    • Jun 2009
    • 77

    Simple Beginner Question about SELECT WHERE

    I have this line, to select a controlled number of values.
    Code:
    $result = mysql_query("SELECT * FROM AFL_BoE_Chapters WHERE ((boe_type='Chapter Index List') AND ('boe_required_level' >= '".$lvl."' )) ORDER BY boeID ASC")
    $lvl is gotten from a number passed on via sessions.
    It is used as a 'level' for the user.
    So this result is supposed to select only the content that is of the same level as the user, or higher.

    The problem is that it selects content which level is above the user, though it works otherwise, but if you might know why this happens.
  • dgreenhouse
    Recognized Expert Contributor
    • May 2008
    • 250

    #2
    At first look, I think you should drop the single-quotes surrounding the column name: boe_required_le vel.

    i.e. 'boe_required_l evel' >= '".$lvl."' ) should be: boe_required_le vel >= '".$lvl."' )
    - or should be: boe_required_le vel >= ".$lvl." ) if boe_required_le vel and $lvl are numeric


    The way the query is constructed,'boe_required_l evel' is always going to be >= '1', and you'll select all records.

    Basically, you're only testing a string against a string - not a column against a value.

    At least that's how it looks to me...

    Comment

    • PHPstarter
      New Member
      • Jun 2009
      • 77

      #3
      boe_required_va lue is a database row. I only put on the single-quotes to check if it would do any difference, when it didnt work without them.
      And yeah $lvl is numeric, it can be from 1 to anything higher, depending on what level the user is set to.

      I tried echoing the values, and they are all set, but it seems as if the $lvl inside the query maybe isn't, since it selects everything. So could it be a syntax error?
      Usually, using '".$lvl."' single-quotes and double-quotes and punctuation always worked..

      Edit: I made it work, I had to turn the arrow operator the other way around, to check for 'equal and lower' instead of 'equal and higher'. lol

      It will now only display the content which match the user's level, setting the max level at the user's level and will display anything below or equal to it.

      Thanks dgreenhouse.

      Comment

      Working...