PDO如何回声多少次更新


PDO How to echo how many updated

我如何回复使用这段代码更新了多少条记录?我知道我可以在一个普通的更新中做到这一点:

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

但这里的问题是,当同时有多个更新时,如何做到这一点。

关于可能重复的注意:我已经解释了我的问题不同于:PDO是在执行语句期间受影响的行该解决方案适用于单个更新,在此代码中不起作用。我想问的是当有多个更新时如何计数。

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();
}

试试这个

当您执行多个->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();
}

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

这个问题在这里也已经问过了:https://stackoverflow.com/a/10522575/2853903