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

pdo rowcount() returns 0
pdo rowcount not working
mysqli_num_rows in pdo
pdo::query
how to delete a row in mysql using php
php row count
pdo number of rows deleted
undefined property: pdostatement::$num_rows

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());
}

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.

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.

So i want something like this

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";}
}

Any ideas?


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

MYSQL DELETE PDO : how I know if row was deleted?, 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​"); Using PDO in PHP gives you the ability to use rowCount(); which returns a count of all rows affects by the last query. Something like the code  WE can delete a record using delete query through PDO connection We assumed that config.php file is already available and it is connected to database . mem_id is the unique id of the record which is to be deleted. In the above code one record will be deleted from the table based on the id number stored in the variable $mem_id.


Deleting rows with the PDO object., A tutorial on how to delete database rows with the PDO object. In this example, I will connect to MySQL using the PDO object before deleting a specific row. For the sake of Let's say that we have a table called cars and that we want to delete every car that is made by Honda. As you can see, it's actually pretty simple! If I *can't* figure out any way to make PDO tell me the truth about whether a delete worked or not, I suppose I'll have to do a second query to see if the row is indeed gone after the delete - and then hope that the second query doesn't lie to me too! Here are the two files you'll need to try this yourself.


A simple trick is to check the number of row affected

$count = $stmt->rowCount();

There is another answer, more detailled here : What does a successful MySQL DELETE return? How to check if DELETE was successful?

PHP PDO If statement to check if field is empty in delete statement, 4 answers 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  There are several ways to run a SELECT query using PDO, that differ mainly by the presence of parameters, type of parameters, and the result type. I will show examples for the every case so you can choose one that suits you best.


You could try somethign like this:

if($stmt[0]->rowCount() > 0)
{ 
echo "success";
}
else{
echo "failure";
}

PDOStatement::rowCount - Manual, If the last SQL statement executed by the associated PDOStatement was a PDOStatement::rowCount() returns the number of rows affected by a DELETE Instead, use PDO::query() to issue a SELECT COUNT(*) statement with the and rowCount() will tell you how many rows your update-query actually found/​matched. As we are searching for deleted data in table Location, we can see this in the last row. We can find the table name in the "AllocUnitName" column. The last row says a DELETE statement has been performed on a HEAP table 'dbo.Location' under transaction ID 0000:000004ce. Now capture the transaction ID from here for our next command. Step 5


PHP MySQL Delete Data, Delete Data From a MySQL Table Using MySQLi and PDO the DELETE syntax: The WHERE clause specifies which record or records that should be deleted. This article will show you how to execute CRUD(Create, Retrieve, Update, Delete) in PHP using PDO. The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP. When you use PHP for web development, you can use either mysqli or PDO, but, this page will focus on PDO. Pre-requisite Before …


Delete row from database - PHP, I am trying to delete an item from my database but it isnt working. stop immediately using the mysql_ functions, and switch to mysqli_ or pdo. Instead of deleting a row you've selected, you just delete the first one in the table? I would echo the $id variable before the delete query to see if in fact that first  Prepare a DELETE statement for execution. Pass values to the statement using the bindValue () method of the PDOStatement object. Call the execute () method of the PDOStatement object to execute the delete statement. Call the rowCount () method of the PDOStatement to get the number of rows deleted.


PHP Cookbook, Using PDO::PARAM_LOB effectively depends on your underlying database. to know how many rows were changed by an INSERT, UPDATE, or DELETE query. $st->rowCount(); $st->execute(array('Luca Brasi')); print "Deleted rows: " . If you're issuing a SELECT statement, the only foolproof way to find out how many  Rowcount to get number of rows changed or affected by Query. WE can use rowcount () to know number of rows or records affected by the latest sql statement involving any Delete , update , insert command. This function is to be used immediately after the sql execute command. This function works similar to mysql_affected_rows function.