PDO MySQL“不在()”不​​工作

时间:2016-06-25 17:14:19

标签: php mysql pdo

我有一个查询可以返回给定视频中的相关视频节目,因此同样的流派,国家,发布日期等。

我想使用video_names功能排除某些not in ()。问题是查询返回我想要排除的视频,它们也在数组中排序。为什么会这样?

public function interval($month, $not, $limit) {
    $this->not = array_unique($not);
    $i = implode(',', $this->not);

    echo $i;//prints: onajr,babyjem,posh

    $query = '
        select title, artists, published, views, video_name, yt_id, duration, play_start, genre, country from videos
        where
        published BETWEEN :published - INTERVAL :month MONTH AND :published + INTERVAL :month MONTH
        and MATCH(country) AGAINST(:country IN boolean mode)
        and MATCH(genre) AGAINST(:genre IN boolean mode)
        and
        video_name not in (" :i ")
        ORDER BY RAND() limit :limit
    ';

    $run_query = $this->pdo->prepare($query);

    $run_query->bindValue(':published', $this->published);
    $run_query->bindValue(':country', '+' . $this->data->country);
    $run_query->bindValue(':genre', '+' . $this->data->genre);
    $run_query->bindValue(':limit', $limit, PDO::PARAM_INT);
    $run_query->bindValue(':month', $month, PDO::PARAM_INT);
    $run_query->bindValue(':i', $i);

    $run_query->execute();
    $data =  $run_query->fetchAll(PDO::FETCH_ASSOC);

    print_r($data);
    //contains all three of them onajr,babyjem,posh
}

1 个答案:

答案 0 :(得分:3)

您可以创建参数数组和值,如下所示:

$this->not = array_unique($not);
$i = array();
foreach( $this->not as $key => $val ) {
    $i[':vid_' . $key] = $val;
}

将其绑定到查询:

' ..... video_name not in ('. implode(',', array_keys($i)) .') ....'

然后,绑定params:

foreach( $i as $key => $val ) {
    $run_query->bindValue($key, $val);
}