Querying for distinct values ​​where id is in a list of values ​​- gives me a wrong result
P粉659516906
P粉659516906 2024-03-31 23:37:37
0
1
443

$('#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);
}

I have all six IDs in the table, each with a different mail

So the result should be 6 instead of 0

please help

P粉659516906
P粉659516906

reply all(1)
P粉298305266

I think your problem lies in your php function

Maybe $ids is an array, so you have to use implode to convert it to a string

like this

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);
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template