首页  >  问答  >  正文

查询不同的值,其中id在一个值列表中 - 给我一个错误的结果

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

我在表中拥有所有六个 ID,每个 ID 都有不同的邮件

所以结果应该是 6 而不是 0

请帮忙

P粉659516906P粉659516906182 天前357

全部回复(1)我来回复

  • P粉298305266

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

    我认为你的问题出在你的 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);
    }

    回复
    0
  • 取消回复