I have a script that I am using to retrieve zip codes within a certain radius using a foreach php statement. However, with these zip codes that I am getting, I want to add a comma between each value (for example: 11111,22222,33333) to use in a SQL query using the command "IN". I have figured out how to get a comma but for some reason, it is putting a comma before the first zip code which is causing my next SQL statement to break.
foreach ($zips as $key => $value) {
$space = ",";
$fields = $space.$key;
echo "$fields";
}
This is the output I get:
,11111,22222,33333
It is mostly right as there is no comma at the end.
This is the query I am trying to run as well which keeps breaking:
SELECT * FROM customers WHERE zip IN ($fields)
Any help would be greatly appreciated or if there is somehow a way where you can run the query with just a space between the values.