使用 PHP 更新 mysql 中受影响的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1487675/
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
row(s) affected in mysql update with PHP
提问by An employee
How do i find if my update is successful or not? i update using a where uniqueName=name so i should always only update 0 rows or 1. What is a good way to check if i updated a row or not?
我如何知道我的更新是否成功?我使用 where uniqueName=name 进行更新,因此我应该始终只更新 0 行或 1 行。检查我是否更新了一行的好方法是什么?
回答by brianreavis
mysql_affected_rows()will return the number of rows affected by your update.
mysql_affected_rows()将返回受更新影响的行数。
http://us.php.net/manual/en/function.mysql-affected-rows.php
http://us.php.net/manual/en/function.mysql-affected-rows.php
回答by Karthikeya
There is really no way of knowing it. Let's say the table tbl_numbers(id, value) has rows (1,"one") and (2,"two");
真的没有办法知道它。假设表 tbl_numbers(id, value) 有行 (1,"one") 和 (2,"two");
$result1="update tbl_numbers set value='first' where id=1";
If you check $result1 in if clause it returns true and mysql_affected_rows() returns 1.
如果您在 if 子句中检查 $result1,它将返回 true,而 mysql_affected_rows() 返回 1。
However, for $result2="update tbl_numbers set value='two' where id=2";If you check $result2 in if clause it returns true and mysql_affected_rows() returns 0.
但是,$result2="update tbl_numbers set value='two' where id=2";如果您在 if 子句中检查 $result2,它将返回 true,而 mysql_affected_rows() 返回 0。
And, for $result3="update tbl_numbers set value='three' where id=3";If you check $result3 in if clause it returns true and mysql_affected_rows() returns 0.
并且,$result3="update tbl_numbers set value='three' where id=3";如果您在 if 子句中检查 $result3,则它返回 true,而 mysql_affected_rows() 返回 0。
回答by Josh Leitzel
回答by rubayeet
try using mysql_affected_rows([$link])
回答by NullDivision
So what if the user resubmits data that is already the same as the information in the DB? If memory serves that would return mysql_affected_rows() with a value of 0 because by definition no rows were updated in the process. Yet the query itself was successful.
那么如果用户重新提交的数据已经与数据库中的信息相同呢?如果内存可用,将返回值为 0 的 mysql_affected_rows() ,因为根据定义,该过程中没有行被更新。然而查询本身是成功的。
The workaround to this would be to either proof your content before insertion or using:
解决方法是在插入或使用之前证明您的内容:
$result = mysql_query($q);
if( mysql_affected_rows() == 0 )
$state = $result ? "Success" : "Fail";
This way you get to check if the rows were updated and if not you can check if it was a malfunction or just repeat data.
通过这种方式,您可以检查行是否已更新,如果没有,您可以检查是故障还是重复数据。
回答by NullDivision
I'm pretty late here, but you could always force a failure on no match by changing your query.
我在这里已经很晚了,但是您总是可以通过更改查询来强制不匹配失败。
For table 'foo' with columns 'ID' and 'value'. Match expression: ID = 4
对于带有“ID”和“值”列的表“foo”。匹配表达式:ID = 4
update foo
join (select ID as nearID,
ID = 4
as matched from foo order by matched desc limit 0, 1) as t on nearID = ID
set value='somedata2'
, ID = if(matched, nearID, 'nomatch')
It's probably easier just to change the table to include either an update timestamp or sequence number instead though. (since those would change on every update)
不过,将表更改为包含更新时间戳或序列号可能更容易。(因为这些会在每次更新时改变)
回答by Usman Zaheer
mysql_affected_rows($link) is correct but depreciated.. mysqli_affected_rows($link) should be used now.
mysql_affected_rows($link) 是正确的,但折旧了。现在应该使用 mysqli_affected_rows($link)。
回答by Aris
If using PDO, affected rows can be obtained using rowCount()function as below:
如果使用 PDO,可以使用rowCount()函数获取受影响的行,如下所示:
/* Delete all rows from the FRUIT table */
$del = $dbh->prepare('DELETE FROM fruit');
$del->execute();
/* Return number of rows that were deleted */
print("Return number of rows that were deleted:\n");
$count = $del->rowCount();
print("Deleted $count rows.\n");
回答by kmchmk
If you are using prepared statement..
如果您使用的是准备好的语句..
//query
$stmt = $conn->prepare("UPDATE user SET name = 'Hyman' WHERE id = ?");
//bind parameters
$stmt->bind_param("i", $id);
//execute
$stmt->execute();
//updated row count
echo $stmt->affected_rows;
echo " rows updated";
回答by rael_kid
if your update query is in a variable named $query, you can do;
如果您的更新查询在名为 $query 的变量中,您可以这样做;
$result = mysql_query($query);
if($result){
//succes!
} else {
//fail!
}
Hope this helps. If you just want to know if your query was executed succesfully, this will do. If you really want to know how many rows are affected, use the other suggestions.
希望这可以帮助。如果您只是想知道您的查询是否成功执行,那么就可以了。如果您真的想知道有多少行受到影响,请使用其他建议。

