[英]PDO Select query using array of OR
I have an array of objects:我有一个对象数组:
$arr = array('10', '12');
and i am running this query using PDO:我正在使用 PDO 运行此查询:
$stmt = $pdo->prepare("SELECT * from table WHERE user_sequence = :user_sequence");
i want to be able to have the query say WHERE user_sequence = :item1 OR user_sequence = :item2
我希望能够让查询说WHERE user_sequence = :item1 OR user_sequence = :item2
item1, item2
etc being the objects from the array item1, item2
等是数组中的对象
how can i do this using PDO?我如何使用 PDO 做到这一点?
usually in MySQL, i would do:通常在 MySQL 中,我会这样做:
$sql="SELECT * from table WHERE ";
foreach($array as $a) {
$sql.="col = '".$a."' OR ";
}
but im not sure how to copy this theory in a PDO query但我不知道如何在 PDO 查询中复制这个理论
You could do it like this:你可以这样做:
$values = [ 'first value', 'second value', 'third value' ];
$conds = array_map(function() {
return "user_sequence = ?";
}, $values);
$sql = "SELECT * from table WHERE " . implode(" OR ", $conds);
$stmt = $pdo->prepare($sql);
$stmt->execute($values);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.