为什么PDO rowCount()在UPDATE表之后返回0而不修改现有数据?

Dre*_*din 18 php

我正在阅读有关如何使用PHP将数据插入和更新到MySQL表的教程,代码如下所示.我的问题是当我点击更新但我没有修改任何数据时,rowCount()返回0并破坏代码.

我的问题是,如果我只是使用数据库中的相同值更新数据库,为什么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>
Run Code Online (Sandbox Code Playgroud)

cee*_*yoz 21

我的问题是,如果我只是使用数据库中的相同值更新数据库,为什么rowCount()返回零?

rowCount 通过查询计算受影响的行.由于您没有更改任何内容,因此受影响的行数为零.

PDOStatement-> rowCount - 返回受上一个SQL语句影响的行数

  • 如果数据尚未修改,则rowCount将为零.如果数据已修改,则rowCount将为1或更高.如果出现错误,则rowCount将为null或false或非零值. (3认同)

Cro*_*zin 12

它与PHP无关 - 它只是MySQL的工作方式.

MySQL文档说:

对于UPDATE语句,默认情况下受影响的行值是实际更改的行数.如果在连接时指定了CLIENT_FOUND_ROWS标志,则受影响的行值是"找到"的行数; 也就是说,由WHERE子句匹配.mysql_real_connect()mysqld

  • PDO版本:`$ connection = new PDO($ data_source,$ user,$ password,array(PDO :: MYSQL_ATTR_FOUND_ROWS => true));` (5认同)