dtid30526 2017-08-16 16:41
浏览 62
已采纳

cookie中的数组,使用pdo查询访问

I have an array of id's in a cookie. When I print the cookie array it prints like this: ["39","580"]. I need to access id 39 and 580.

I have tried everything I need to use this in a query like this:

    $queryordenar = $db->prepare("SELECT id FROM property 
    WHERE id IN (:list) ORDER BY price ASC");
    $queryordenar->execute(array(
    'list' => $array
     ));

Thanks

  • 写回答

1条回答 默认 最新

  • douchen9569 2017-08-16 16:50
    关注

    You can't pass an array as a parameter. You need to explode the array, pass in a ? for every value, and then bind the correct value to each placeholder:

    $bindPlaceholders = [];
    foreach ($array as $val) {
        $bindPlaceholders[] = "?";
    }
    $bindString = "(".implode(",", $bindPlaceholders).")";
    $queryordenar = $db->prepare("SELECT id FROM property WHERE id IN ".$bindString." ORDER BY price ASC");
    foreach ($array as $i => $val) {
        $queryordenar->bindValue($i + 1, $val); // binding starts at 1
    }
    $queryordenar->execute();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码