tags:

views:

43

answers:

2

Is it possible to assign php array in MySQL IN() function? for example,

$numbers = array('8001254656','8886953265','88864357445','80021536245');
$sql = mysql_query("SELECT * FROM `number_table` WHERE `number` IN ($numbers)");

Any Ideas?

Thanks,

+1  A: 

No. Use implode() first.

Ignacio Vazquez-Abrams
+3  A: 

This code snippet should help you out, as long as number is restricted to numbers.

<?php

// Generate a filtered comma-separated list of the numeric values in numbers.
// Non-numeric values might allow for SQL-injection bugs.
$SQL_numberList = implode(", ", array_filter($numbers, 'is_numeric'));
$sql = mysql_query("SELECT * FROM `number_table` WHERE `number` IN ($SQL_numberList)");

For string arguments in the list you'll need to do something a little different.

Sam Minnée