PDO bind array to Where IN

后端 未结 4 1677
执念已碎
执念已碎 2021-01-23 18:29

I want to bind a array of Strings to the WHERE IN part of a SQL command, which I want to run afterwards on a SQL Server. The problem is probably that I try to bind

4条回答
  •  轮回少年
    2021-01-23 19:02

    You could use some string manipulation.

    You can count the number of ? you'd need by using str_repeat("?", count(explode(",", $refIdsPartial))). This will create your placeholders.

    $totalCount = 
    "SELECT referral, COUNT(username) AS cnt FROM accounts
    WHERE referral IN (". str_repeat("?,", count(explode(",", $refIdsPartial))-1) . "?) GROUP BY referral";
    

    Now that the placeholders are in place, you can explode the , from the string and execute

    $ps_totalCounts->execute( explode(",", $refIdsPartial) );
    

提交回复
热议问题