如何使用PDO知道DELETE查询是否确实删除了一行 [英] How to know if the DELETE query really deletes a row ,using PDO

查看:255
本文介绍了如何使用PDO知道DELETE查询是否确实删除了一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PDO语句将DELETE查询传递给mysql.我想知道查询是否真正删除了一行?我尝试使用$stmt->execute()

I am passing a DELETE query to mysql using PDO statements. I want to know, whether the query really deletes a row or not? I try to use return value of $stmt->execute()

function delete(){
    $stmt = $this->db->prepare("DELETE FROM users WHERE id= :id");
    $stmt->bindValue(':id',$_POST[id]);

    var_dump($stmt->execute());
}

但这总是给出true,即使没有行被删除.即使我不发布任何值,或者一次又一次发布相同的值,这也是如此.可能是因为删除0行后查询成功,所以$stmt->execute()的返回值无济于事.

But This gives always true, even no row is deleted. This gives true even if I don't post any value Or even post same value again and again. May be because query is successful with 0 row deleted , so return value of $stmt->execute() does not help.

PDO中还有其他方法可以知道是否确实删除了任何行,更具体地说,我想确保仅删除了一行.

So Is There any other way in PDO to know whether any row is really deleted or not, more specifically I want to be sure that only one row was deleted.

所以我想要这样的东西

function delete(){
    $stmt = $this->db->prepare("DELETE FROM users WHERE id= :id");
    $stmt->bindValue(':id',$_POST[id]);
    $stmt->execute();
    if($rowdeleted == 1){ echo "success";}
    else{echo "failure";}
}

有什么想法吗?

推荐答案

如果您使用的是PDO,则需要 rowCount 方法. 如果是mysqli,则需要受影响的行

If you are using PDO then you need the rowCount method. In case of mysqli you need the affected rows

这篇关于如何使用PDO知道DELETE查询是否确实删除了一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆