我有一个查询,返回相关的视频标题,从一个给定的视频,所以相同的流派,国家,出版日期等。
我想使用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
}

最佳答案

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

$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)) .') ....'

然后,绑定参数:
foreach( $i as $key => $val ) {
    $run_query->bindValue($key, $val);
}

10-08 15:33