簡體   English   中英

PDO MySQL“ not in()”不起作用

[英]PDO MySQL “not in()” not working

我有一個查詢,該查詢返回給定視頻的相關視頻標題,因此,相同體裁,國家/地區,發布日期等。

我想使用not in ()函數排除某些video_names 問題是查詢返回我要排除的視頻,並且它們也首先在數組中排序。 為什么會這樣呢?

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM