PDO使用OR数组选择查询

时间:2015-11-11 21:56:37

标签: php mysql pdo

我有一个对象数组:

$stmt = $pdo->prepare("SELECT * from table WHERE user_sequence = :user_sequence");

我正在使用PDO运行此查询:

WHERE user_sequence = :item1 OR user_sequence = :item2

我希望能够让查询说出item1, item2

$sql="SELECT * from table WHERE "; foreach($array as $a) { $sql.="col = '".$a."' OR "; } 等是数组中的对象

我怎么能用PDO做到这一点?

通常在MySQL中,我会这样做:

    Dim con As String = ConfigurationManager.ConnectionStrings("Model1").ConnectionString
    Dim db As New DataContext(con)
    Dim a As Table(Of Catagory2)
    a = db.GetTable(Of Catagory2)()
    Dim dt As List(Of Catagory2) = a.AsQueryable.Where(Function(p) p.Town.Contains("something")).ToList()

但我不确定如何在PDO查询中复制此理论

1 个答案:

答案 0 :(得分:1)

你可以这样做:

$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);