select distinct where id in a list of values - 给我一个错误的结果

select distinct where id in a list of values - gives me a wrong result

$('#btnmails').on('click', function(){
    let ids = $.map($('.atitle'), (e) => $(e).attr('data-id'));
    ids = JSON.stringify(ids);
    console.log(ids);  // ["26","25","24","23","22","21"]
    $.post('pro.php', {fn: 'btn_mails', args: [ids]}, function(data){
        console.log(data);  // 0 - but it is not true
    });
});

pro.php

function btn_mails($ids){
    global $db;
    $sq = "select distinct mail from clients where id in ('" . $ids . "')";
    $st = $db->prepare($sq);
    $st->execute();
    $arr = $st->fetchAll();
    echo count($arr);
}

我在 table 中拥有所有六个 ID,每个 ID 都有不同的邮件
所以结果应该是 6 而不是 0
请帮忙

我认为你的问题出在你的php函数

可能 $ids 是一个数组,因此您必须使用 implode

将其转换为字符串

像这样

function btn_mails($ids){
    global $db;
    $sq = "select distinct mail from clients where id in (" . implode(', ', $ids) . ")";
    $st = $db->prepare($sq);
    $st->execute();
    $arr = $st->fetchAll();
    echo count($arr);
}