PHP计算一些行

时间:2017-02-27 11:48:33

标签: php

我遇到了问题,我想创建一个从数据库中计算数据的函数。但我希望从列中计算一些ID,而不是所有行。

我的代码不起作用:

function countMember() {
    include("./config/config.php");
    $fetch_countMember =   $fetch_countMember->prepare("SELECT * FROM `accounts`");
    $fetch_countMember->execute();
    while($fetch_countMember=   $fetch_countMember->fetch()) {
    if($fetch_countMember["member"] == 5 || $fetch_countMember["member"] == 6 || $fetch_countMember["member"] == 12 || $fetch_countMember["member"] == 13 || $fetch_countMember["member"] == 14 || $fetch_countMember["member"] == 15 || $fetch_countMember["member"] == 16 || $fetch_countMember["member"] == 17 || $fetch_countMember["member"] == 20 || $fetch_countMember["member"] == 21)
        $c_member++;
    }
    return $c_member;
}

1 个答案:

答案 0 :(得分:1)

您需要使用PDO::exec()

优雅的方式:

$pdo = new PDO(....);
$ids = [5,6,12,13,14,15,16,17,20,21];
$query = sprintf("SELECT * FROM accounts WHERE member IN (%s)", implode($ids, ','));
$count = $pdo->exec();
echo $count;