Search code examples
phpselectpropel

Using WHERE IN with prepared SQL statements in Propel


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.

A simple example what I'm trying to do is this:

$c = Propel::getConnection();
$q = $c->prepare("SELECT foo FROM bar WHERE blah IN(:ids)");
$q->execute(array(':ids' => array(1, 2, 3, 4, 5)));

Obviously, this isn't working for me. I've also tried using implode(',' array(1, 2, 3, 4, 5)) as my ':ids' parameter, which also doesn't work. The first method complains of having to accept an array, and the second method complains of comparing a string to an integer.

Is there a proper way to pass an array of values to a query for a WHERE IN? I don't want to hard code it, as that would mean preparing it for every iteration of a loop.

Note that I don't really have the option of creating a criteria and using doSelect instead, as the query is actually far more complicated than the example given here and I seriously doubt I could build it that way.

Many thanks!


Solution

  • You cannot use a named parameter marker of the same name twice in a prepared statement. You cannot bind multiple values to a single named parameter in, for example, the IN() clause of an SQL statement.

    PDO::prepare

    Propel uses PDO

    Propel site