search

Home  >  Q&A  >  body text

Querying for distinct values ​​where id is 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);
}

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粉659516906P粉659516906241 days ago436

reply all(1)I'll reply

  • P粉298305266

    P粉2983052662024-04-01 15:21:43

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

    reply
    0
  • Cancelreply