成功的 MySQL DELETE 返回什么?如何检查 DELETE 是否成功? [英] What does a successful MySQL DELETE return? How to check if DELETE was successful?

查看:98
本文介绍了成功的 MySQL DELETE 返回什么?如何检查 DELETE 是否成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 PHP,我试图删除一条记录,但我想检查它是否成功.是否从成功的 DELETE FROM foo where bar = 'stuff' 返回了什么?

Using PHP, I am trying to delete a record, but I want to check if it was successful or not. Is anything returned from a successful DELETE FROM foo where bar = 'stuff'?

或者,您知道检查 DELETE 是否成功的其他方法吗?或者我最好在删除之前确保该行存在?如果可能,我试图避免另一个查询.

Alternatively, do you know any other ways to check if a DELETE was successful? Or am I better off just making sure the row exists before I delete it? I am trying to avoid another query if possible.

推荐答案

假设您正在使用 mysql_query:

对于其他类型的 SQL 语句,INSERT、UPDATE、DELETE、DROP 等,mysql_query() 在成功时返回 TRUE,在错误时返回 FALSE.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

如果您使用的是 PDO::exec,那么手册是这样说的:

If you are using PDO::exec, then the manual says this:

PDO::exec() 返回由您发出的 SQL 语句修改或删除的行数.如果没有行受到影响,PDO::exec() 返回 0.

PDO::exec() returns the number of rows that were modified or deleted by the SQL statement you issued. If no rows were affected, PDO::exec() returns 0.

不想回答 snipe,但由于它被选为答案,我应该注意 mysql_query 将返回 TRUE 即使查询实际上没有删除任何内容.您应该使用 mysql_affected_rows 来检查.

Don't want to answer snipe, but since this was selected as the answer, I should note that mysql_query will return TRUE even if the query did not actually remove anything. You should use mysql_affected_rows to check for that.

这篇关于成功的 MySQL DELETE 返回什么?如何检查 DELETE 是否成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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