Search code examples
phppdowhere-in

How can I bind parameters in a PHP PDO WHERE IN statement


Params:

$params = 2826558;                        # Necessary Object
$params = array(2826558,2677805,2636005); # NULL

Execution code:

    $data = $this->DQL_selectAllByCampaign_id()
                 ->execute( array($params) )
                 ->fetchAll();

    var_dump( $data );

SQL Query:

$this->DQL_selectAllByCampaign_id = $this->conn->prepare(

        "SELECT * FROM `banner` WHERE  `campaign_id` IN (?)"

);

If $params is Integer, returns necessary Object. If $params is Array, returns NULL.

After all, in fact it should work... How can I do that?


Solution

  • I'm quite sure this isn't the 'right' answer, but we solved this by adding in count($array) placeholders. Then using call_user_func_array we pass the params in.

    Thanks for asking this - will be interesing to find out what the proper way to do this is...