PDO Statement and WHERE IN() Clause

Take a look at the following example, and pay attention to the bold letter. This is how we solve this, if there any suggestion, please leave a comment bellow :

  public static function get_detail_gl_activity($start_date="",$end_date="",$account_code="", $ctype=array()) {
    $pdo = Doctrine_Manager::getInstance()->getCurrentConnection()->getDbh();

    $i = 0 ;
    $query = "SELECT ALL cnofaktur, ckode_coa, account, cketerangan,
              ndebit, ncredit, dtanggal, cdepot, ctype
              FROM vdetail_gl
              WHERE ckode_coa LIKE :account_code
              AND dtanggal BETWEEN :start_date AND :end_date AND ctype IN(".utilities::pdo_param_repeat(":ctype",count($ctype)).") ";
    $stmt = $pdo->prepare($query);

    $params = array(
          "account_code"  => "%".$account_code."%",
          "start_date"    => date::date2sql($start_date),
          "end_date"      => date::date2sql($end_date)  
    );
    
    $j = 0 ;
    foreach($ctype as $key=>$value) {
      $params["ctype".$j++] = $value ;
    }    

    $stmt->execute($params);
    $result = $stmt->fetchAll();
          
    return $result ;
  }


utilities.class.php

.....
public static function pdo_param_repeat($string, $multiplier) {
$ret = "";
for($i=0;$i<$multiplier;$i++) {
if ($i) $ret.=",";
$ret.=$string.$i;
}

       return $ret ;
   }
.....


Comments