SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SQL Query Using IN and a Array

    Hey guys. I'm somewhat new to php and having a bit of trouble here.

    Code:
    $test = array_keys($zips); 
     
    function show_array($array) {
        foreach ($array as $value) {
            if (is_array($value)) {
                show_array($value);
            } else {
                echo $value . " ";
            }
        }
    }
    $tests = show_array($test);
     
    $sql = "SELECT street, city, state, zip, community, bed, bath, features, picture FROM properties WHERE zip IN ('$tests')";
    I'm not sure how to get the "$tests" array correctly in the sql query. This way doesnt seem to work but also doesnt spit out any errors and selects 0 records. The show_array($test) puts out the zips like this:

    28043 28019 28160 28139 28074 28076 28040 28024

    Hope this is somewhat understandable. Thanks for your help!

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Two things here,one take $test out of the single quote and two you will need to implode $text with a comma between values for the sql syntax to be right.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, ok I figured it out now. Thanks so much!

    Code:
    $test = array_keys($zips); 
     
    $tests = implode(",", $test);
     
    $sql = "SELECT street, city, state, zip, community, bed, bath, features, picture FROM properties WHERE zip IN ($tests)";


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •