Forum Moderators: coopster
if (in_array($zip,$ziparray))
{
sql = "select * from table1 where zip = $zip";
}
Your first statement is a bit confusing, at least to me.
i have an array of zip codes that need to be separated by commas
If the zip codes are already in an array, why would you need comma separation? The in_array function will find them in the array.
Perhaps, and I'm speculating - what you have is a list of zips that you need to put into an array?
// skip this if your zips are already quoted
foreach($ziparray as $index => $zip)
$ziparray[$index]="'".$zip."'";
// build a string of comma-separated zips
$zipstring=implode(',', $ziparray);
And the SQL would be
select * from table1 where zip in ($zipstring);
Hope this is what you are looking for.
I already have the zip codes in an array. I didn't know that you could find zips with the in_array function. Thanks for that. I was looking for a comma separated list of zips that I could insert into my mysql IN statement (similar to what frizhard suggested). Whichever way will do the trick is fine with me.
i implemented your code and I got the following error:
Wrong datatype for second argument in call to in_array
I'm using php 4.0.6.
suggestions?
looks like you are trying to call functions with a variable that is not set, or is not an array.
you can check if your variable is set and is an array with
if(isset($ziparray)==FALSE ¦¦ is_array($ziparray)==TRUE)
{
// variable is set and is an array.
// SQL query here
}
else
{
// var not set or not an array
// show an error message or something
}
Also you should make sure your array has elements, otherwise $zipstring = implode(',',$ziparray); would return an empty string an you would get a SQL error in your select