What does a successful MySQL DELETE return? How to check if DELETE was successful?

Asked
Active3 hr before
Viewed126 times

9 Answers

90%

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.,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., 1 @MaxZoom If there's an error, mysql_query will return FALSE. If there's nothing to delete, mysql_query would return TRUE and mysql_affected_rows would be 0. That said, you shouldn't be using mysql_query at all anymore. – ceejayoz Nov 16 '17 at 15:07 ,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'?

you could try this for your php code, placed after your query runs:

if (mysql_affected_rows() > 0) {
    echo "You have successfully updated your data.<br><br>";
}
else {
    echo "The data you submitted matched the current data so nothing was changed.<br><br>";
}
88%

PHP uses mysqli query() or mysql_query() function to delete records in a MySQL table. This function takes two parameters and returns TRUE on success or FALSE on failure.,Required - SQL query to delete records in a MySQL table.,If you want to delete a record from any MySQL table, then you can use the SQL command DELETE FROM. You can use this command at the mysql> prompt as well as in any script like PHP.,The following code block has a generic SQL syntax of the DELETE command to delete data from a MySQL table.

The following code block has a generic SQL syntax of the DELETE command to delete data from a MySQL table.

DELETE FROM table_name[WHERE Clause]
load more v
72%

$sql = "DELETE FROM art WHERE id='5'";
if ($db - > query($sql) === TRUE) {
   $message = "good"; // all worked good.
} else {
   $message = "bad"; // didnt work because something was wrong
}
65%

The DELETE statement deletes rows from tbl_name and returns the number of deleted rows. To check the number of deleted rows, call the ROW_COUNT() function described in Section 12.16, “Information Functions”. , These statements use all three tables when searching for rows to delete, but delete matching rows only from tables t1 and t2. , DELETE is a DML statement that removes rows from a table. , Select the rows not to be deleted into an empty table that has the same structure as the original table:

Single-Table Syntax

DELETE[LOW_PRIORITY][QUICK][IGNORE] FROM tbl_name[[AS] tbl_alias]
   [PARTITION(partition_name[, partition_name]...)]
   [WHERE where_condition]
   [ORDER BY...]
   [LIMIT row_count]

Multiple-Table Syntax

DELETE[LOW_PRIORITY][QUICK][IGNORE]
tbl_name[.*][, tbl_name[.*]]...
   FROM table_references[WHERE where_condition]

DELETE[LOW_PRIORITY][QUICK][IGNORE]
FROM tbl_name[.*][, tbl_name[.*]]...
   USING table_references[WHERE where_condition]

If the DELETE statement includes an ORDER BY clause, rows are deleted in the order specified by the clause. This is useful primarily in conjunction with LIMIT. For example, the following statement finds rows matching the WHERE clause, sorts them by timestamp_column, and deletes the first (oldest) one:

DELETE FROM somelog WHERE user = 'jcole'
ORDER BY timestamp_column LIMIT 1;

Select the rows not to be deleted into an empty table that has the same structure as the original table:

INSERT INTO t_copy SELECT * FROM t WHERE...;

Use RENAME TABLE to atomically move the original table out of the way and rename the copy to the original name:

RENAME TABLE t TO t_old, t_copy TO t;

Drop the original table:

DROP TABLE t_old;

For the first multiple-table syntax, only matching rows from the tables listed before the FROM clause are deleted. For the second multiple-table syntax, only matching rows from the tables listed in the FROM clause (before the USING clause) are deleted. The effect is that you can delete rows from many tables at the same time and have additional tables that are used only for searching:

DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id = t2.id AND t2.id = t3.id;

Or:

DELETE FROM t1, t2 USING t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id = t2.id AND t2.id = t3.id;

The preceding examples use INNER JOIN, but multiple-table DELETE statements can use other types of join permitted in SELECT statements, such as LEFT JOIN. For example, to delete rows that exist in t1 that have no match in t2, use a LEFT JOIN:

DELETE t1 FROM t1 LEFT JOIN t2 ON t1.id = t2.id WHERE t2.id IS NULL;

If you declare an alias for a table, you must use the alias when referring to the table:

DELETE t1 FROM test AS t1, test2 WHERE...

Correct:

DELETE a1, a2 FROM t1 AS a1 INNER JOIN t2 AS a2
WHERE a1.id = a2.id;

DELETE FROM a1, a2 USING t1 AS a1 INNER JOIN t2 AS a2
WHERE a1.id = a2.id;

Incorrect:

DELETE t1 AS a1, t2 AS a2 FROM t1 INNER JOIN t2
WHERE a1.id = a2.id;

DELETE FROM t1 AS a1, t2 AS a2 USING t1 INNER JOIN t2
WHERE a1.id = a2.id;
load more v
75%

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error. ,If you are using PDO::exec, then the manual says this:,Assuming you are using mysql_query:,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.

php
load more v
40%

It is considered a good practice to escape the values of any query, also in delete statements.,Notice the WHERE clause in the DELETE syntax: The WHERE clause specifies which record(s) that should be deleted. If you omit the WHERE clause, all records will be deleted!,Escape values by using the placeholder %s method:,The mysql.connector module uses the placeholder %s to escape values in the delete statement:

Important!: Notice the statement: mydb.commit(). It is required to make the changes, otherwise no changes are made to the table.

mydb.commit()
load more v
22%

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns true on success or false on error. , For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or false on error. ,mysql_error() - Returns the text of the error message from previous MySQL operation, mysql_query() will also fail and return false if the user does not have permission to access the table(s) referenced by the query.

load more v
60%

Post your comments , suggestion , error , requirements etc here

my_cursor = my_connect.cursor() #
my_cursor.execute("DELETE FROM  student WHERE  id=25")
my_connect.commit()
print("Rows Deleted = ", my_cursor.rowcount)
my_connect.close()
load more v
48%

For DB2 and MySQL you have the option of inserting one row at a time or multiple rows at a time by including multiple VALUES lists:,You want to delete duplicate records from a table. Consider the following table:,Use the UPDATE statement to modify existing rows in a database table. For example:,By using a WHERE clause with the DELETE command, you can delete a subset of rows in a table rather than all the rows.

	insert into dept(deptno, dname, loc)
	values(50, 'PROGRAMMING', 'BALTIMORE')
	/* multi row insert */
	insert into dept(deptno, dname, loc)
	values(1, 'A', 'B'),
	   (2, 'B', 'C')
load more v

Other "undefined-undefined" queries related to "What does a successful MySQL DELETE return? How to check if DELETE was successful?"