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:
好的,所以我一直在为我正在研究的项目使用PDO包装器,而我正试图找出DELETE查询是否成功。这是我正在使用的代码:
/**
* 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'.
问题是,无论DELETE查询是否实际删除了一行,$ this-> database-> getNumAffected($ result)始终返回'0'。
You can check out the wrapper, but basically $this->database->getNumAffected($result) simply returns exactly the same value PDO::exec() would return.
你可以查看包装器,但基本上$ this-> database-> getNumAffected($ result)只返回PDO :: exec()返回的完全相同的值。
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.)
我尝试了没有包装器的代码(直接进入PDO),我遇到了同样的问题,但反过来了:它总是返回'1'(是否删除了一行。)
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.
编辑:根据这个问题,我做的一切都正确......我不明白为什么这不起作用。
2 个解决方案
#1
2
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:
它没有按预期工作,因为你正在使用的'包装器'不会使用PDO :: exec() - 它将所有内容包装在PDO语句中。根据您提供的URL快速阅读'database'类的2.2.6版源代码,'query'方法应该返回一个包含语句句柄的数组:
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()
.
因此,假设您的$ this-> database-> query()正在调用此数据库类的查询方法,您应该能够执行$ 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()
.
请注意,您对早期响应的断言“由于rowCount()函数存在的错误而导致[您正在使用的包装器使用不同版本的rowCount()”不正确 - 包装器实现了numRows,但是这与PDOStatement :: rowCount()不同,它在database :: query()返回的语句句柄中是完整的。
#2
7
$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;
#1
2
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:
它没有按预期工作,因为你正在使用的'包装器'不会使用PDO :: exec() - 它将所有内容包装在PDO语句中。根据您提供的URL快速阅读'database'类的2.2.6版源代码,'query'方法应该返回一个包含语句句柄的数组:
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()
.
因此,假设您的$ this-> database-> query()正在调用此数据库类的查询方法,您应该能够执行$ 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()
.
请注意,您对早期响应的断言“由于rowCount()函数存在的错误而导致[您正在使用的包装器使用不同版本的rowCount()”不正确 - 包装器实现了numRows,但是这与PDOStatement :: rowCount()不同,它在database :: query()返回的语句句柄中是完整的。
#2
7
$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;