What Does a Successful MySQL Delete Return? How to Check If Delete Was Successful

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

Assuming you are using mysql_query:

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:

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.

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.

How to know if the DELETE query really deletes a row ,using PDO

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

PHP - How to verify record deleted successfully

You can use mysqli.affected-rows.

Consider the following:

$query="delete from exam where eid='$eid'";
if ($con->query($query)==TRUE && $con->affected_rows > 0) {
echo " record deleted";
} else {
echo "Error: " . $query . "<br>" . $con->error;
}

MYSQL DELETE PDO : how I know if row was deleted?

Have a look at PDO::exec, first example is what you are looking for :

/* Delete all rows from the FRUIT table */
$count = $dbh->exec("DELETE FROM fruit");

/* Return number of rows that were deleted */
print("Deleted $count rows.\n");

How to check if deletion was successful in the database?

First of all: do not ever directly build SQL queries from user input, use prepared statements instead. If you don't know about SQL Injection, you should.

If you are using JDBC, you can check the result of #executeUpdate() to see how many rows were affected. If it was zero, then you can say that it was a wrong id.

This is the method definition:

public int executeUpdate(java.lang.String sql)

The return value is:

An int that indicates the number of rows affected, or 0 if using a DDL statement.


In the program at hand, you can just simply do this:

int deleted = st.executeUpdate(query);
if (deleted == 0) {
JOptionPane.showMessageDialog(null, "Nothing to delete!");
return;
}

MySQL : DELETE query return when rows are not found

Use mysqli_affected_rows function to find the number of records affected by the previous query.

Confirming deletion of record in mysql php

You don't need to check by boolean value 0 or 1. But if you want to do it that way, you will need to declare a flag before that and then change the flag value according to condition after that you can do it [as your way]

(mysqli_affected_rows($conn) == 1)

But as default a simple if() conditon will check and return true on pass and false on fail.

So, simply you can do:

if($res)
{
//your codes if query is done
} else
{
//your codes if query fails
}

How to make sure delete query removes a row?

With the help of rowCount();

PDOStatement::rowCount() returns the number of rows affected by the
last DELETE, INSERT, or UPDATE statement executed by the corresponding
PDOStatement object.

if ( $stmt->rowCount() ) {
// activating the user
} else {
// your token is invalid
}


Related Topics



Leave a reply



Submit