我在SO上找到了这个代码,它非常适合将PDO和IN()语句一起使用.
$values = explode(',',$values) ; # 1,4,7 $placeholders = rtrim(str_repeat('?,count($values)),') ; $query = "SELECT * FROM table WHERE id IN ($placeholders)"; $stm = $db->prepare($query) ; $stm->execute($values) ;
$query = "SELECT * FROM table WHERE id IN ($placeholders) AND product=?"; $stm = $db->prepare($query) ; $stm->execute(array($values,$product)) ; //error happens when adding product placeholder
我以为这会工作,但我得到:
警告:PDOStatement :: execute()[pdostatement.execute]:sqlSTATE [HY093]:参数号无效:绑定变量的数量与第3行中的令牌数不匹配($stm行)
任何想法如何让这样做的意图?
UPDATED执行到数组,仍然不工作..