PDO 如何回显更新了多少

PDO How to echo how many updated

如何回显使用此代码更新了多少条记录? 我知道我可以在公共更新中做到这一点:

echo $stmt->rowCount() . " records UPDATED successfully"; 

但是这里的问题是,同时有多个更新的时候怎么办

关于可能重复的注意事项:我已经解释过我的问题不同于:PDO were rows affected during execute statement 该解决方案适用于单个更新,在此代码中不起作用。我问的是多次更新时如何统计

try {
    require_once 'connexion.php';

    $stmt = $conn->prepare("UPDATE bookmarks
                            SET podcast=:podcast, text=:text
                            WHERE id=:id");
    $stmt->bindParam(':podcast', $podcast);
    $stmt->bindParam(':text', $text);
    $stmt->bindParam(':id', $id);

    $podcast = 1;
    $text = "text 1";
    $id = 147;
    $stmt->execute();

    // another row:
    $podcast = 2;
    $text = "text 2";
    $id = 265;
    $stmt->execute();

    // echo " records UPDATED successfully";
}

catch(PDOException $e){
    echo $e->getMessage();
}

尝试$stm->rowCount()。来源:http://php.net/manual/en/pdostatement.rowcount.php

这里也有人问过这个问题:

试试这个。

当你做多个 ->execute() 时,你必须在你做的时候捕获每个 ->execute() 的计数,然后在最后显示累积的总计数

try {
    require_once 'connexion.php';
    $update_count = 0;

    $stmt = $conn->prepare("UPDATE bookmarks
                            SET podcast=:podcast, text=:text
                            WHERE id=:id");
    $stmt->bindParam(':podcast', $podcast);
    $stmt->bindParam(':text', $text);
    $stmt->bindParam(':id', $id);

    $podcast = 1;
    $text = "text 1";
    $id = 147;
    $stmt->execute();

    $update_count += $stmt->rowCount();


    // another row:
    $podcast = 2;
    $text = "text 2";
    $id = 265;
    $stmt->execute();

    $update_count += $stmt->rowCount();


    echo "$update_count records UPDATED successfully";
}

catch(PDOException $e){
    echo $e->getMessage();
}