vote up 0 vote down star

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,

flag

50% accept rate

2 Answers

vote up 3 vote down check

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.

link|flag
vote up 1 vote down

No. Use implode() first.

link|flag

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.