1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

WHERE equalities

Discussion in 'Databases' started by mnymkr, Jun 20, 2007.

  1. #1
    I am impoding an array into my query


    what is the sql statement for something like

    where color = red or color=blue and shape = square OR shape = circle

    I want to return only results where there are red and blue squares and circles
     
    mnymkr, Jun 20, 2007 IP
  2. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    SELECT * FROM 
      mytable
    WHERE
      color IN ('red', 'blue')
    AND
      shape IN ('circle', 'square')
    Code (markup):
    i tested the code above on my server and it worked :)
     
    UnrealEd, Jun 20, 2007 IP
  3. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #3
    that works like a charm but let me ask you

    is there a way to say IN (ALL) null treats it as null and % doesn't work.

    i want to set the variable to be IN ALL if the variable is not posted.

    i tried searching around but couldn't find much
     
    mnymkr, Jun 20, 2007 IP
  4. flippers.be

    flippers.be Peon

    Messages:
    432
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    then you have to make it a dynamic sql statement

    either build up the sql statement in php or whatever you are doing and leave out the where part where you don't have a parameter for

    or make a stored procedure that gets color and shape as input params, checks them for null and creates a dynamic statement and executes it
     
    flippers.be, Jun 20, 2007 IP
  5. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #5
    that is what i was thinking but i can't quite figure out what to do with my WHERE statement,

    I could just go in and say if the variable is not set then load all values but i am trying to make this as universal as possible
     
    mnymkr, Jun 20, 2007 IP
  6. flippers.be

    flippers.be Peon

    Messages:
    432
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    if you don't know color then just have:

    WHERE shape IN ('circle', 'square')

    and if you don't know shape then only have:

    WHERE color IN ('red', 'blue')

    be careful if you don't have anything at all
    1. leave out 'where' when you're building a sql string
    2. it'll return your whole database
     
    flippers.be, Jun 20, 2007 IP
  7. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #7
    I really am having trouble with the logic here.

    I can easily do it if I put all values in for IN if the GET varialbe is not set however I am trying to make this code universal

    So then I thoght I would make the WHERE only appear if the GET value was set but because there is more than one, then WHERE would have to appear on the first one and the others would need AND

    any ideas?
     
    mnymkr, Jun 21, 2007 IP
  8. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Do you want the code to be in php? or another language? There are some ways to do it: you can use an array to store the differrent conditions, and then just implode it after all the checking.

    here's an example of how you can do it in php:
    // check if any GET variables are set:
    if (isset($_GET) && sizeof($_GET) > 0) {
      $sql_conditions = array();
      if (isset($_GET['shape']) {
        $sql_conditions[] = "IN('" . implode("', '", $_GET['shape']) . "')"; // i don't know what the value of the shape textfield is, but for now i assume it's an array
      }
      if (isset($_GET['color']) {
        $sql_conditions = "IN('" . implode("', '", $_GET['color']) . "')"; // i don't know what the value of the shape textfield is, but for now i assume it's an array
      }
    
      if (sizeof($sql_conditions) > 0) {
        $sql = "WHERE ".implode(' AND ', $sql_conditions);
      }
    } else {
      $sql = NULL;
    }
    
    // now run the query
    PHP:
     
    UnrealEd, Jun 21, 2007 IP
  9. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #9
    nice code

    i didn't think about using implode on the WHERE. Thanks


    now i just have to rebuild my get statement for the sort and it is ready freddy....
     
    mnymkr, Jun 21, 2007 IP
  10. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #10
    just did it and it worked great!

    i can even recreate my GET url with some minor modifications

    would it be hard to add 2 text box to this like a From [text box] To [text box]
     
    mnymkr, Jun 21, 2007 IP
  11. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #11
    What do you mean exactly? add some extra conditional statements to the query?
     
    UnrealEd, Jun 21, 2007 IP
  12. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #12
    Yeah here is the problem and you fixed 90% of it


    we wanted the multiple select forms that you code allowed but now we need to allow the user to put in a range for a group of numbers, if they want to.

    so there will be an another AND or it could be a WHERE if no other GET variable are set

    the next statement I would need to add would be

    WHERE/AND number BETWEEN GET[] AND GET[]
     
    mnymkr, Jun 21, 2007 IP
  13. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #13
    if both numbers need to be set, it's pretty easy:
    if (isset($_GET['start_number']) && isset($_GET['end_number'])) {
      $sql_conditions[] = "BETWEEN " . intval($_GET['start_number']) . " AND " . $_GET['end_number'];
    }
    PHP:
    you can just add this after:
      if (isset($_GET['color']) {
        $sql_conditions = "IN('" . implode("', '", $_GET['color']) . "')"; // i don't know what the value of the shape textfield is, but for now i assume it's an array
      }
    PHP:
    If at least one number should be set, it's gonna be a little trickier:
    if (isset($_GET['start_number']) && isset($_GET['end_number'])) {
      $sql_conditions[] = "field BETWEEN " . intval($_GET['start_number']) . " AND " . $_GET['end_number'];
    }
    elseif (isset($_GET['start_number']) && !isset($_GET['end_number'])) {
      $sql_conditions[] = "field > " . intval($_GET['start_number']);
    }
    elseif (!isset($_GET['start_number'])  && isset($_GET['end_number'])) {
      $sql_conditions[] = "field < " . intval($_GET['end_number']);
    }
    PHP:
     
    UnrealEd, Jun 21, 2007 IP
  14. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #14
    hey you are helping me on both threads. i really appreciate. i have learned alot in the last two days
     
    mnymkr, Jun 21, 2007 IP
  15. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #15
    is that intval to protect again sql injections?
     
    mnymkr, Jun 21, 2007 IP
  16. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #16
    No, not really: intval is a function that grabs the integer value of a certain variable. It will help in this case as it will remove all non-numeric characters from the entered string. So when i enter 123-hi-homie, intval will return 123.
    To prevent SQL injection with strings, you have to use mysql_real_escape_string or mysql_escape_string (sometimes mysql_real_escape_string is not available). here are the links for more information on these functions (they don't work as i can't post active links yet :mad:):
    http://www.php.net/mysql_real_escape_string
    http://www.php.net/mysql_escape_string

    PS: I added some extra stuff to my previous post
     
    UnrealEd, Jun 21, 2007 IP
  17. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #17
    man you have the patience of Job

    but you truly are helping me alot

    I see what you added. Does that take care of if they put the larger value in for the FROM etc?

    Also I am curoius, I am tryng not to use a drop down for the To and From form fields

    however if i use a text box, it always appears in my URL with a null , is there a way to prevent this or would i have to set some defualt values or something.
     
    mnymkr, Jun 21, 2007 IP
  18. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #18
    my pleasure :)

    No, not atm, but there's an easy way to fix that. Just replace this:
    if (isset($_GET['start_number']) && isset($_GET['end_number'])) {
      $sql_conditions[] = "field BETWEEN " . intval($_GET['start_number']) . " AND " . $_GET['end_number'];
    }
    PHP:
    by this:
    if (isset($_GET['start_number']) && isset($_GET['end_number'])) {
      $getValues = array($_GET['start_number'], $_GET['end_number']);
      sort($getValues);
      $sql_conditions[] = "field BETWEEN " . intval($getValues[0]) . " AND " . $getValues[1];
    }
    PHP:
    You see, what it does is it creates an array which contains both the start and the end number. the function sort will sort the values from small to large, therefore the first number (array index = 0) will always be the smallest of the two, and the second number is the highest one (array index = 1)
    what do you mean with "null": the string "null", or just an empty value. Like this:
    www.my_url.com/page.php?to=&from=1
    or
    www.my_url.com/page.php?to=null&from=1
    If it's the first one, than there's no problem. php converts from string to ints in no time, and an empty string is the same as the integer 0 for php. if it's the second one, you will have to add another if to check if the value doesn't equal "null"
     
    UnrealEd, Jun 21, 2007 IP
  19. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #19
    i see, so the text entries will appear in the URL not matter whta
    mine appears this way

    www.my_url.com/page.php?to=null&from=1

    I juts need to set defualt values, but my database is ever chaning so i am not sure what my maximum would be
     
    mnymkr, Jun 21, 2007 IP
  20. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #20
    are you submitting your form using javascript? cause if you just let html handle the submission, no value should be entered, just like the url i posted first:
    www.my_url.com/page.php?to=&from=1

    Don't ever trust default values :). NEVER trust a visitor: you have to check the value in your form processing page, no matter what :)
     
    UnrealEd, Jun 21, 2007 IP