What are the best workarounds for using a SQL IN clause with prepared statement, eg. PDO?

In a system I'm working on that uses Propel for handling the database, I'm preparing a raw query that needs to accept a varying WHERE IN condition.

googled somewhere,i found:
This is the PDO Workaround.

$ids = array(1, 2, 3, 4, 5);

$inQuery = implode(',', array_fill(0, count($ids), '?'));

$c = Propel::getConnection();
$q = $c->prepare("SELECT foo FROM bar WHERE id IN( {$inQuery} )");

// bindValue starts at 1 (not 0)
foreach ($ids as $key => $val)
{
  $q->bindValue(($key+1), $val);
}

$q->execute();


Date: 2012-01-16 08:41:06 and last modified: 2012-01-16 08:42:16

Hot tags: