tags:

views:

131

answers:

2

Hello everyone,

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.

A: 
$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;
dmitrig01
Hmm, the wrapper that I am using uses a different version of rowCount() because of an error that exists with the rowCount() function. The numRows() in the wrapper simply does a SELECT COUNT(*) query, so this will not work for the wrapper.
John
+1  A: 

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().

TML
I'm not sure what you mean by it doesn't use exec(), hit Ctrl + F and type execute (. Thanks for the answer though.
John
I mean that it doesn't use PDO::exec(), but uses PDOStatement::execute(), which is an entirely different thing. It'd probably be useful to go to php.net/pdo and learn the difference between the PDO object (and its 'exec()' method) and the PDOStatement object (and its 'execute()' method). There are some really critical differences there.
TML
Ah, okay, that makes sense. Thanks very much for the answer, and I will look into that. I don't know that much about PDO, as when I started learning it (which was prompted by the person who made this wrapper,) I was immediately given a wrapper to use instead. The only advantage of the wrapper I see is that you have to type variables, so if it's the wrong type (e.g. a string when it should be an integer,) it returns an error.Oh, I meant to say this in my first comment: I understand that numRows() is very different from rowCount(), and because of the way numRows() works, I don't use it.
John