'select distinct where id in a list of values - gives me a wrong result [duplicate]
$('#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 table and each of them with a distinct mail
so result should be 6 and not 0
pls help
Solution 1:[1]
I think that your problem is in your php function
Probably $ids is an array so you have to transform it in a string using implode
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);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | R4ncid |
