Page 1 of 1

php array for mysql statement

Posted: Fri Feb 26, 2010 8:13 am
by lokesh_kumar_s
let us assume
$ssn={001,002,003}
and
i want to execute ike
select username from city where ssn in (001,002,003)
but i am having the vaues inside array $ssn i want to do like this
select username from city where ssn in ($ssn)

now tell me how do i convert $ssn to 001,002,003 please suggest if i could use list.

Re: php array for mysql statement

Posted: Fri Feb 26, 2010 9:52 am
by Kurby
You need to destruct your array into a string. There is no handy premade function to do this. You should loop through the array and append them to a string.

Code: Select all

 $string = '';
$first = true;
foreach($array as $val)
{
     if(!$first)
         $string .= ', ';
    $string .= $val
} 

Re: php array for mysql statement

Posted: Fri Feb 26, 2010 10:06 am
by AbraCadaver
Handy pre-made function: 8)

Code: Select all

$string = implode(',', $ssn);

Re: php array for mysql statement

Posted: Fri Feb 26, 2010 10:18 am
by Kurby
AbraCadaver wrote:Handy pre-made function: 8)

Code: Select all

$string = implode(',', $ssn);
Well there ya go :)