PHP SQL select 在 IN 语句中只使用数组的第一个结果

PHP SQL select uses only the first result of the array in IN statement

我正在尝试根据从我放入数组中的 table 中选择的 ID 对列求和。由于某些原因,在 Where 子句中仅使用第一个 ID。当我回显变量时,所有 id 都在那里。我做错了什么?

$counttheid = array();
$stmt3 = $mysqli->prepare("SELECT 
    id 
    FROM account
    WHERE  level <= '5' AND door = ? AND `group_name` = ? AND betaald = 'Yes'");
    $stmt3->bind_param("ss",$usernamesession,$groupname);
    $stmt3->execute();
    $result3 = $stmt3->get_result(); //only works when nd_mysli is set on the server!
    

    while ($rowid = $result3->fetch_assoc())
{
    $counttheid[] = $rowid['id'];
    $countid = implode(',', $counttheid); // contains all the ids !!


}

$sql = "SELECT SUM(mobcash) AS totalcash FROM account WHERE id IN (?)  
     ";
    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param("i",$countid);
    $stmt->execute();
    $stmt->bind_result($row['totalcash']);
    while($stmt->fetch()) $sumcash = $row['totalcash'];

    echo $sumcash; // Somhow only the sum of the first ID of the array !!
    
    echo $countid;// all the ids from the array !!

不仅是输入,绑定参数的数量也需要匹配。

使用此示例尝试从 whileexecute 的代码:

while ($rowid = $result3->fetch_assoc())
{
    $counttheid[] = $rowid['id'];
    // $countid = implode(',', $counttheid); // contains all the ids !!
}

$in = str_repeat('?,', count($counttheid) - 1) . '?';
$types = str_repeat('i', count($counttheid));
$sql = "SELECT SUM(mobcash) AS totalcash FROM account WHERE id IN ($in)";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param($types, ...$counttheid);
$stmt->execute();

bind_param处,...$counttheid的部分,...的部分是argument unpacking operator