Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I retrieve the number of rows deleted with PDO?

Tags:

php

mysql

pdo

Okay, so I have been using a PDO wrapper for a project I'm working on, and I'm trying to find out whether a DELETE query was successful or not. Here is the code I am using:

/**
* A pretty straight-forward query to delete a row from the verification
* table where user_id is $user_id and code is $code
*/
$result = $this->database->query("DELETE FROM verification " .
                                 "WHERE user_id = %u AND code = %s",
                                 $user_id,
                                 $code);

/**
 * This function will grab the PDO's exec() return, which should
 * return the number of rows modified.
 */
if($this->database->getNumAffected($result) > 0)
    return true;
else
    return false;

The problem is, whether the DELETE query actually deletes a row or not, $this->database->getNumAffected($result) always returns '0'.

You can check out the wrapper, but basically $this->database->getNumAffected($result) simply returns exactly the same value PDO::exec() would return.

I tried this code without the wrapper (directly into PDO,) and I had the same problem but reverse: it always returned '1' (whether a row was deleted or not.)

Any help would be greatly appreciated.

EDIT: Based on this SO question, I'm doing everything right... I don't understand why this isn't working.

like image 328
John M. Avatar asked Jan 03 '10 21:01

John M.


People also ask

How can I count deleted records in MySQL?

You can use ROW_COUNT() function to check the number of deleted rows. The conditions in the WHERE clause (optional) identify which rows to delete. Without WHERE clause, all rows are deleted. If you specify the ORDER BY clause, the rows are deleted in specified order.

How can I count rows in PHP?

We can get the total number of rows in a table by using the MySQL mysqli_num_rows() function. Syntax: mysqli_num_rows( result ); The result is to specify the result set identifier returned by mysqli_query() function.

How do you the delete a row with PDO?

Steps for deleting data from PHP PDOPass 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.

What does delete query return in SQL?

The standard DELETE statement in SQL returns the number of deleted rows.


2 Answers

$query = $this->database->prepare("DELETE FROM verification WHERE user_id = :user_id AND code = :code", array('user_id' => $user_id, 'code' => $code));
$query->execute();

if ($query->rowCount() > 0) {
  return TRUE;
}
return FALSE;
like image 52
dmitrig01 Avatar answered Oct 18 '22 10:10

dmitrig01


It doesn't work as you expect because the 'wrapper' that you're using doesn't ever use PDO::exec() - it wraps everything in a PDO statement. According to a quick read of the source code for version 2.2.6 of the 'database' class from the URL you provided, the 'query' method should return an array which contains the statement handle:

502 $statement = $this -> getDatabaseConnection () -> prepare ( $query );
...
587 $ret = array ( $statement, func_get_args (), $lastIndex );
588     
589 return ( $ret );

So, assuming your $this->database->query() is calling this database class' query method, you should be able to do $result[0]->rowCount().

Note that your assertion to the earlier response that "the wrapper that [you are] using uses a different version of rowCount() because of an error that exists with the rowCount() function" is not true - the wrapper implements a numRows, but this is not the same thing as PDOStatement::rowCount(), which is intact inside of the statement handle returned from database::query().

like image 41
TML Avatar answered Oct 18 '22 09:10

TML