如何使用IN语句设置预准备查询

时间:2015-05-10 作者:LAL

对于如何使用准备好的查询设置SQL IN语句,我有点困惑。代码如下:

$in = implode(\',\', array_fill(0, count($product_ids), \'%d\'));
        $results = $this->db->get_results($this->db->prepare("SELECT ID, post_title FROM {$this->db->posts} WHERE ID NOT IN({$in}) AND post_type=%s ORDER BY ID DESC LIMIT %d", array(implode(\',\', $product_ids), "post", $num)));
结果返回null。我错在哪里?

更新:修复了我自己的问题。以下是我的解决方案:

$prepare = array();
        $in = implode(\',\', array_fill(0, count($product_ids), \'%d\'));
        foreach ($product_ids as $ids){
            $prepare[] = $ids;   
        }
        $prepare[] = "post";
        $prepare[] = $num;
        $results = $this->db->get_results($this->db->prepare("SELECT ID, post_title FROM {$this->db->posts} WHERE ID NOT IN({$in}) AND post_type=%s ORDER BY ID DESC LIMIT %d", $prepare));

1 个回复
SO网友:LAL

我的问题解决方案:

$prepare = array();
        $in = implode(\',\', array_fill(0, count($product_ids), \'%d\'));
        foreach ($product_ids as $ids){
            $prepare[] = $ids;   
        }
        $prepare[] = "post";
        $prepare[] = $num;
        $results = $this->db->get_results($this->db->prepare("SELECT ID, post_title FROM {$this->db->posts} WHERE ID NOT IN({$in}) AND post_type=%s ORDER BY ID DESC LIMIT %d", $prepare));

结束