douhan9191 2016-06-08 21:52
浏览 55

使用预准备语句解释PDO IN子句

I am reading this answer: PHP - Using PDO with IN clause array

It uses the code:

$in_array = array(1, 2, 3);
$in  = str_repeat('?,', count($in_array) - 1) . '?';
$sql = "SELECT * FROM my_table WHERE my_value IN ($in)";
$stm = $db->prepare($sql);
$stm->execute($in_array);
$data = $stm->fetchAll();

Can anyone explain why they used

$in = str_repeat('?,', count($in_array) - 1) . '?';

instead off:

$in = str_repeat('?,', count($in_array));

I am puzzled and cannot figure out the logic behind the trailing - 1) . '?'

  • 写回答

1条回答 默认 最新

  • duangonglian6028 2016-06-09 00:08
    关注

    This is because str_repeat() function repeats a string a specified number of times and count() function will return the length of the array which means that it will result in "?,?,?,?,?," if the array length is 5(let suppose) which doesn't looks formatted that's why count($in_array)-1 with '?' appended had been used.

    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献