Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Feb 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Best way to Query DB for array contents

    What do you think is the best/most efficient way to do this?

    I'm pulling zip codes from a database, based on latitude/longitude coordinates, the resulting zip codes I'll put into an array...

    From there I want to grab matches from another database....I want to pull each record that matches any of those zip codes. I can cycle through the array and do an individual call for each zip code in the array, but it seems inefficient to do so many calls...

    Is there a way I can search for any of them in one query? Or some better way I can arrange this?

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    How big will this array be? If it's under, say, 100, you can build the query syntax using the "IN" keyword, as in SELECT col FROM tbl WHERE zipcode IN ($var, $var, $var). Use a loop to build the query text from the array.

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Once you have your array of zip codes, make your next query like so:
    PHP Code:
    $sql 'SELECT * FROM `table` WHERE `zip` IN ( \'' implode'\', \''$zipcodes ) . '\' )'

  • #4
    New to the CF scene
    Join Date
    Feb 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Most often it would probably range around 150 to 250. But it could be 2000 or more zip codes in the array. Most calls may turn up no results.

  • #5
    New to the CF scene
    Join Date
    Feb 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I figured there was some code like that I could use.
    Thanks guys, I'll try it out!


  •  

    Posting Permissions

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