PDOStatement::bindParam で一括処理する時、なんか動かないと思ったら

PDOStatement::bindParam – php.net
http://www.php.net/manual/ja/pdostatement.bindparam.php#98145

// This works ($val by reference):
foreach ($params as $key => &$val) {
    $sth->bindParam($key, $val);
}
?>

// This will fail ($val by value, because bindParam needs &$variable):
foreach ($params as $key => $val) {
    $sth->bindParam($key, $val);
}

参照渡しじゃないとダメ。

こんな感じ?

$pdo = new PDO($dsn, $username, $password, $options);
$sth = $pdo->prepare($sql);
foreach ($params as $key => &$val) {
    $key++;
    
    if (is_bool($val)) {
        $data_type = PDO::PARAM_BOOL;
    } elseif (is_null($val)) {
        $data_type = PDO::PARAM_NULL;
    } elseif (is_int($val)) {
        $data_type = PDO::PARAM_INT;
    } elseif (is_string(val)) {
        $data_type = PDO::PARAM_STR;
    } else {
        $data_type = false;
    }
    
    $sth->bindParam($key, $value, $data_type);
}
$sth->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);