Web Analytics Made Easy -
StatCounter (PHP) Mysql select where `field` = 4,6,7,8,9,10,11,12,13 or 14 - CodingForum

Announcement

Collapse
No announcement yet.

(PHP) Mysql select where `field` = 4,6,7,8,9,10,11,12,13 or 14

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • (PHP) Mysql select where `field` = 4,6,7,8,9,10,11,12,13 or 14

    I have a users table containing there level, and i need to run a Sql to see if a members level is either 4,6,7,8,9,10,11,12,13 or 14.

    My SQL atm is

    PHP Code:
    $Sql "SELECT user_id, username, password FROM members WHERE username = '$username' AND password = '$password'"
    Would i have to write them all out e.g.
    PHP Code:
    $Sql "SELECT user_id, username, password FROM members WHERE username = '$username' AND password = '$password'"; AND `level` = '4' OR username '$username' AND password '$password'"; AND `level` = '6' OR  username = '$username' AND password = '$password'"; AND `level` = '7'  etc etc etc 
    Or is there a better way to do it?

  • #2
    You didn't include 5 in your list, is that intentional?

    If it was:
    Code:
    $Sql = "SELECT user_id, username, password FROM members WHERE username = '$username' AND password = '$password'" AND (level = 4 OR level BETWEEN 6 AND 14)
    If it wasn't:
    Code:
    $Sql = "SELECT user_id, username, password FROM members WHERE username = '$username' AND password = '$password'" AND level BETWEEN 4 AND 14
    If your level field is a number you don't need to put quotes around the number you are comparing against. MySQL will let you get away with doing it but other SQL dialects will not so it isn't a good habit to get into.
    Last edited by oracleguy; Sep 11, 2011, 06:21 PM.
    OracleGuy

    Comment


    • #3
      Yeah i didnt include it on purpose, Thanks for that, ill give it a go

      Comment


      • #4
        And that works when most of your range is sequential.

        But you can also do this:

        Code:
        $Sql = "SELECT user_id, username, password FROM members "
             . " WHERE username = '$username' AND password = '$password' "
             . " AND [COLOR="Red"]level IN (4,6,7,8,9,10,11,12,13,14)[/COLOR] ";
        Which would be more practical if your list were, for example, (4,23,99,107,388,1011,1033) or such.
        Be yourself. No one else is as qualified.

        Comment


        • #5
          That could also be userful for my site too, thanks . I guess IN means "is in this list of values"?

          Comment


          • #6
            Originally posted by tomharto View Post
            That could also be userful for my site too, thanks . I guess IN means "is in this list of values"?
            Yes. NOTE: If the field in question is *NOT* numeric, then you have to put apostrophes around *each* value:
            Code:
            ... WHERE name IN ('joe','ann','bob','kate')
            Be yourself. No one else is as qualified.

            Comment


            • #7
              Okay, thanks

              Comment

              Working...
              X
              😀
              🥰
              🤢
              😎
              😡
              👍
              👎