...

View Full Version : Best way to Query DB for array contents



Astarael
02-29-2008, 06:02 PM
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?

Fumigator
02-29-2008, 06:13 PM
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.

kbluhm
02-29-2008, 06:15 PM
Once you have your array of zip codes, make your next query like so:


$sql = 'SELECT * FROM `table` WHERE `zip` IN ( \'' . implode( '\', \'', $zipcodes ) . '\' )';

Astarael
02-29-2008, 07:30 PM
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.

Astarael
02-29-2008, 07:33 PM
I figured there was some code like that I could use.
Thanks guys, I'll try it out!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum