Forum Moderators: coopster

Message Too Old, No Replies

array and sql statement question

how do i separate array values with a comma?

         

danielm28

2:19 am on Feb 13, 2005 (gmt 0)

10+ Year Member



i have an array of zip codes that need to be separated by commas and inserted into the following sql statement:

select * from table1 where zip in ($ziparray);

How do I do this programmatically?

Thank you

grandpa

4:28 am on Feb 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



For the select statement, you might try something like this.
First, find out if the zip is in the array, and if it is then use it in your select statement.

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?

frizhard

4:22 pm on Feb 13, 2005 (gmt 0)

10+ Year Member



Hi, this is what I do in cases like this:

// 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.

danielm28

4:46 pm on Feb 13, 2005 (gmt 0)

10+ Year Member



frizhard,

i implemented your method and now I get the following 2 messages:

Invalid argument supplied for foreach()

Bad arguments to implode()

suggestions?

danielm28

4:54 pm on Feb 13, 2005 (gmt 0)

10+ Year Member



grandpa,

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?

frizhard

5:31 pm on Feb 13, 2005 (gmt 0)

10+ Year Member



make sure you typed the variable name correctly.

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

frizhard

5:32 pm on Feb 13, 2005 (gmt 0)

10+ Year Member



Ups, correction:

if(isset($ziparray)==FALSE && is_array($ziparray)==TRUE)

it's an AND, not an OR

danielm28

9:31 pm on Feb 14, 2005 (gmt 0)

10+ Year Member



Oops.......

my array name had a capital Z in it instead of a lower case z. That was the cause of these error messages. All fixed now.

Thank you everyone for your help.