Heim  >  Fragen und Antworten  >  Hauptteil

Die Abfrage nach eindeutigen Werten, bei denen sich die ID in einer Werteliste befindet, liefert ein falsches Ergebnis

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

Ich habe alle sechs IDs in der Tabelle und jede ID hat eine andere E-Mail

Das Ergebnis sollte also 6 statt 0 sein

Bitte helfen Sie

P粉659516906P粉659516906182 Tage vor352

Antworte allen(1)Ich werde antworten

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

    Antwort
    0
  • StornierenAntwort