成功的MySQL DELETE返回什么?如何检查DELETE是否成功? [英] What does a successful MySQL DELETE return? How to check if DELETE was successful?
问题描述
使用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,删除,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.
不想回答阻击,但是由于选择了它作为答案,因此我应该注意,即使查询实际上并未删除任何内容,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屋!