我有一个简单的函数可以从 mysql 数据库中删除一行。
function delete_post($post_id){
global $dbh;
$stmt = $dbh->prepare("DELETE FROM mjbox_posts WHERE post_id = ?");
$stmt->bindValue(1, $post_id, PDO::PARAM_INT);
$stmt->execute();
}
我想知道删除尝试后现有函数中是否有一种简单的方法来验证它是否成功? (成功是指该行是否被删除)
最佳答案
function delete_post($post_id){
global $dbh;
$stmt = $dbh->prepare("DELETE FROM mjbox_posts WHERE post_id = ?");
$stmt->bindValue(1, $post_id, PDO::PARAM_INT);
$stmt->execute();
return $stmt->rowCount();
}
但是,我不会仅仅因为没有这样的记录就将未删除任何内容的查询称为不成功。
关于php - 验证删除帖子功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16150876/