php 为什么在不修改现有数据的情况下更新表后 PDO rowCount() 返回 0?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6237212/
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
Why does PDO rowCount() return 0 after UPDATE a table without modifying the existing data?
提问by Drewdin
I am reading a tutorial on how to insert and update data into a MySQL table using PHP, the code is listed below. My problem is when i click update but I have not modified any data, rowCount() returns 0 and breaks the code.
我正在阅读有关如何使用 PHP 将数据插入和更新数据到 MySQL 表的教程,代码如下所示。我的问题是当我单击更新但我没有修改任何数据时,rowCount() 返回 0 并破坏代码。
My question is, If I am simply updating the database with the same values that are in the database, why does rowCount() return zero? My thoughts were that even though it was the same data it would be inserted anyway and return a count of the updated rows? I am guessing that it check the data before it try's the update? Can anyone shed some light on this for me and suggest a workaround? I have been starring at the code for hours and have been unable to come up with anything, thanks.
我的问题是,如果我只是用数据库中的相同值更新数据库,为什么 rowCount() 返回零?我的想法是,即使它是相同的数据,它还是会被插入并返回更新行的计数?我猜它会在尝试更新之前检查数据?任何人都可以为我阐明这一点并提出解决方法吗?我一直在盯着代码几个小时,但一直无法想出任何东西,谢谢。
<?php
require_once('../includes/connection.inc.php');
// initialize flags
$OK = false;
$done = false;
// create database connection
$conn = dbConnect('write', 'pdo');
if (isset($_GET['article_id']) && !$_POST) {
// prepare sql query
$sql = 'SELECT article_id, title, article FROM blog WHERE article_id = ?';
$stmt = $conn->prepare($sql);
// bind the results
$stmt->bindColumn(1, $article_id);
$stmt->bindColumn(2, $title);
$stmt->bindColumn(3, $article);
// execute query by passing array of variables
$OK = $stmt->execute(array($_GET['article_id']));
$stmt->fetch();
}
// if form has been submitted, update record
if (isset($_POST['update'])) {
//prepare update query
$sql = 'UPDATE blog SET title = ?, article = ? WHERE article_id = ?';
$stmt = $conn->prepare($sql);
// execute query by passing array of variables
$stmt->execute(array($_POST['title'], $_POST['article'], $_POST['article_id']));
$done = $stmt->rowCount();
}
// redirect page on sucess or if $_GET['article_id'] not defined
if ($done || !isset($_GET['article_id'])) {
header('Location: http://localhost/PHP_Solutions/admin/blog_list_pdo.php');
exit();
}
// store error message if query fails
if (isset($stmt) && !$OK && !$done) {
$error = $stmt->errorInfo();
if (isset($error[2])) {
$error = $error[2];
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Update Blog Entry</title>
<link href="../styles/admin.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>Update Blog Entry</h1>
<p><a href="blog_list_pdo.php">List all entries </a></p>
<?php if (isset($error[2])) {
echo "<p class='warning'>Error: $error[2]</p>";
echo '<pre>';
print_r($_POST);
print_r($error);
echo '</pre>';
}
if ($article_id == 0) { ?>
<p class="warning">Invalid request: record does not exist.</p>
<?php } else { ?>
<form id="form1" method="post" action="">
<input name="article_id" type="hidden" value="<?php echo $article_id; ?>">
<p>
<label for="title">Title:</label>
<input name="title" type="text" class="widebox" id="title" value="<?php echo htmlentities($title, ENT_COMPAT, 'utf-8'); ?>">
</p>
<p>
<label for="article">Article:</label>
<textarea name="article" cols="60" rows="8" class="widebox" id="article"><?php echo htmlentities($article, ENT_COMPAT, 'utf-8'); ?></textarea>
</p>
<p>
<input type="submit" name="update" value="Update Entry" id="update">
</p>
</form>
<?php } ?>
</body>
</html>
回答by ceejayoz
My question is, If I am simply updating the database with the same values that are in the database, why does rowCount() return zero?
我的问题是,如果我只是用数据库中的相同值更新数据库,为什么 rowCount() 返回零?
rowCount is counting the affectedrows by a query. As you haven't changed anything, there are zero affected rows.
rowCount 正在计算受查询影响的行。由于您没有更改任何内容,因此受影响的行数为零。
PDOStatement->rowCount— Returns the number of rows affected by the last SQL statement
PDOStatement->rowCount— 返回受最后一条 SQL 语句影响的行数
回答by Crozin
It has nothing to do with PHP - it's just how MySQL works.
它与 PHP 无关——它只是 MySQL 的工作方式。
For
UPDATE
statements, the affected-rows value by default is the number of rows actually changed. If you specify theCLIENT_FOUND_ROWS
flag tomysql_real_connect()
when connecting tomysqld
, the affected-rows value is the number of rows “found”; that is, matched by the WHERE clause.
对于
UPDATE
语句,默认情况下受影响的行值是实际更改的行数。如果在连接CLIENT_FOUND_ROWS
到mysql_real_connect()
时指定标志为mysqld
,则受影响的行值是“找到”的行数;也就是说,由 WHERE 子句匹配。
回答by Guilherme Toti
When you're using the UPDATE statement, and you submit the same values that are in database, it will always return zero, because it doesn't affected any row.
当您使用 UPDATE 语句并提交与数据库中相同的值时,它将始终返回零,因为它不会影响任何行。
A way to resolve this problem is:
解决此问题的一种方法是:
$done = $stmt !== false ? true : false;
What i did? I did that:
我做了什么?我是这样做的:
if($stmt !== false){
$done = true;
} else{
$done = false;
}
Because if rowCount() is zero, but $stmthas executed without errors, $stmtwas executed, but didn't change anything.
因为如果rowCount() 为零,但$stmt已执行且没有错误,则$stmt已执行,但没有改变任何内容。
回答by pb149
It's how MySQL works and has nothing intrinsically to do with the PDO extension; performing a regular mysql query would produce the same results. There is a workaround I found using the mysql functions, although I'm not sure if you can do anything similar with a PDO object.
这是 MySQL 的工作方式,与 PDO 扩展没有本质上的关系;执行常规的 mysql 查询会产生相同的结果。我找到了一种使用 mysql 函数的解决方法,尽管我不确定您是否可以对 PDO 对象执行类似的操作。
$q = 'UPDATE etc...';
$r = mysql_query($q, $con);
$info = mysql_info(); // Returns info about last query.
list($matches, $changed, $warnings) = sscanf($matched, "Rows matched: %d Changed: %d Warnings: %d");
if ($matches > 0) {} // etc
Hope this helps a little.
希望这有所帮助。