php PDO 是执行语句期间受影响的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10522520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PDO were rows affected during execute statement
提问by MaurerPower
I have found many ways to use the exec statement for PDO, but I'm not sure it helps me. My understanding is that I have to use the execute() function for prepared statements. I am updating a row with data from user input, so I would like to use a prepared statement instead of the query() call.
我找到了很多方法来使用 PDO 的 exec 语句,但我不确定它对我有帮助。我的理解是我必须对准备好的语句使用 execute() 函数。我正在使用来自用户输入的数据更新一行,因此我想使用准备好的语句而不是 query() 调用。
My code is as follows:
我的代码如下:
$dbh = buildDBConnector();
$sql = "UPDATE tb_users
SET authState=1
WHERE id = ? AND authPass = ?";
$q = $dbh->prepare($sql);
$f = $q->execute(array($id,$authPass));
if($f){
echo '<br />Success<br />';
}else{
echo '<br />Failure<br />';
}
The issue is that the query itself is error free and executes fine, so there is no failure to store in $f. However, I need to know if it actually found the row to update, then successfully updated it. In other words, I need the affected rows. When googling and such, it keeps coming to the exec statement, but from my understanding, exec isn't for prepared statements? Any suggestions?
问题是查询本身没有错误并且可以正常执行,因此存储在 $f 中没有失败。但是,我需要知道它是否确实找到了要更新的行,然后成功更新了它。换句话说,我需要受影响的行。在谷歌搜索等时,它不断进入 exec 语句,但据我所知, exec 不是用于准备好的语句吗?有什么建议?
回答by Marc B
Try $q->rowCount(). Prepared statements will return the number of affected rows via that method.
试试$q->rowCount()。准备好的语句将通过该方法返回受影响的行数。
回答by Martin Thoma
$q->rowCount()returns the number of rows affected by the last (executed) SQL statement where $qis the prepared statement which is often called $stmt.
$q->rowCount()返回受最后(执行的)SQL 语句影响的行数,$q其中通常称为准备好的语句$stmt。
So most users who read this might want something like:
因此,大多数阅读本文的用户可能想要以下内容:
$pdo = new PDO($dsn, $username, $password);
$sql = "UPDATE tb_users SET authState=1 WHERE id = ? AND authPass = ?";
$stmt = $dbh->prepare($sql);
$stmt->execute(array($id, $authPass));
if ($stmt->rowCount()){
echo 'Success: At least 1 row was affected.';
} else{
echo 'Failure: 0 rows were affected.';
}
回答by Bud Damyanov
A side note:when updating a table with identical values rowCount()will return always 0. This is normal behavior. You can change it yourself since PHP 5.3 by creating a PDO object with following attribute:
旁注:更新具有相同值的表时rowCount()将返回 always 0。这是正常行为。自 PHP 5.3 起,您可以通过创建具有以下属性的 PDO 对象来自行更改它:
<? php
$p = new PDO($dsn, $user, $pass, array(PDO::MYSQL_ATTR_FOUND_ROWS => true));
?>
The rowCount()then will return how many rows your update-query actually found/matched.
在rowCount()随后将返回多少行的更新询问居然发现/匹配。
回答by Emre Aydin
PDO's rowCount()from prepared statements returns affected rows if it's a UPDATE, DELETEor INSERTstatement. Otherwise it returns how many rows are returned from SELECTstatement.
rowCount()如果是UPDATE, DELETEorINSERT语句,来自准备好的语句的PDO 会返回受影响的行。否则,它返回从SELECT语句返回的行数。
回答by Do Hoa Vinh
i think PDO rowCount() is useless in MySQL with single UPDATE query. because it always return 0;
我认为 PDO rowCount() 在带有单个 UPDATE 查询的 MySQL 中是无用的。因为它总是返回 0;
ex:
TABLE{id=1, col1="A"}
UPDATE TABLE SET col1="AA" WHERE id=1;
rowCount will return 0;
also
UPDATE TABLE SET col1="AA" WHERE id=999;
rowCount will return 0;
so rowCount() is useless in this case.
i have not tested yet with this query UPDATE TABLE SET col1="AA"
我还没有测试过这个查询 UPDATE TABLE SET col1="AA"

