PDOではINが使えない。なぜか
symfonyで直接SQLを書く事は珍しい事ではない
今日もSQLを直接書くよ!
$sql="select * from hoge where id in(?,?,?)"; $sth = $con->prepare($sql); $sth->execute(1,2,3);
しかし、こう書いても返ってくるのはidが1のレコードのみ。
なぜか。
bindされた後のSQLは
select * from hoge where id in ('1,2,3');
となっており、'1,2,3'を整数と評価し1になるのである。
そうすると結果
select * from hoge where id in (1);
上記SQLが出来上がるというわけだったのだ
SQLインジェクションの恐れがないのであれば
$ids = array(1,2,3); $sql="select * from hoge where id in(".implode(','.$ids).")";
とかけば動く