php array for mysql statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lokesh_kumar_s
Forum Commoner
Posts: 48
Joined: Mon Apr 13, 2009 5:39 am
Contact:

php array for mysql statement

Post 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.
Kurby
Forum Commoner
Posts: 63
Joined: Tue Feb 23, 2010 10:51 am

Re: php array for mysql statement

Post 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
} 
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: php array for mysql statement

Post by AbraCadaver »

Handy pre-made function: 8)

Code: Select all

$string = implode(',', $ssn);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Kurby
Forum Commoner
Posts: 63
Joined: Tue Feb 23, 2010 10:51 am

Re: php array for mysql statement

Post by Kurby »

AbraCadaver wrote:Handy pre-made function: 8)

Code: Select all

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