search

Home  >  Q&A  >  body text

If the condition requires the use of IN, how should I write it? Could you please give me an example?

#生成预处理对象
$sql = 'SELECT * FROM dede_archives WhERE id IN (:id)' ;
$stmt = $pdo -> prepare($sql);
 
 
#参数的绑定并执行
$id = implode(',',[2,3,4,5,6,7,8,9]);
 
#$stmt -> bindParam(':id',$id,PDO::PARAM_STR);
$stmt -> bindValue(':id','2,3,4,5,6,7,8,9',PDO::PARAM_STR);
$stmt -> execute();
 
结果只能查询到id为2的数据,其他id的数据查询不到! 
在传递这些多个值的时候或者多条件的时候应该怎样写;


WeiWei2200 days ago1476

reply all(3)I'll reply

  • Summer

    Summer2018-11-14 18:19:23

    Just splice sql statements

    reply
    0
  • 7tian.work

    7tian.work2018-11-14 10:53:48

    TP5.1.21 After version, array query support:

    To achieve this subquery:

    1. First quote: use think\db\Where;

    2. Define the array: $where = new Where;

    3. You can use it: $where['title'] = ['like', "%".$sotitle."%"];

    Reference address: https://blog.csdn.net/haibo0668/article/details/78295485

    reply
    0
  • Wei

    Well, yours is encapsulated in TP. How can I check if the native PHP should be changed?

    Wei · 2018-11-14 10:57:03
  • Cancelreply