【发布时间】:2015-06-24 04:47:54
【问题描述】:
我刚刚遇到的问题是,
$update_stmt->execute() 没问题,数据库中的数据已经更新
但是,$update_resultrow = $update_stmt->num_rows; 返回 0 ?
我尝试复制 MySQL 命令以在查询中运行,它也运行良好,如下所示:
UPDATE ACCOUNT_EMPLOYEE SET NAME = 'cccccc' WHERE ID = 1
问题代码在这里:
$update_sql = "UPDATE ACCOUNT_EMPLOYEE SET NAME = ? WHERE ID = ?";
if ($update_stmt = $conn -> prepare($update_sql)) {
if ($update_stmt->bind_param("si",
$newname,
$acc_id
)
) {
if ($update_stmt->execute()) {
// must declare here to be able to get num_rows
$update_stmt->store_result();
$update_resultrow = $update_stmt->num_rows;
if ($update_resultrow == 0) {
echo $error_forgot_noresult . '????' . $acc_id ;
$update_stmt->close();
$conn->close();
exit();
}
}
}
}
【问题讨论】:
-
用
$update_stmt->rowCount()替换$update_stmt->num_rows; -
@Umair 那是 PDO,OP 正在使用 mysqli_
-
所以应该是
$update_stmt->num_rows(); -
$update_resultrow = $update_stmt->num_rows;如果执行成功,你想在这里实现什么?如果是这样,您需要使用affected_rows,或阅读手册php.net/manual/en/mysqli-result.num-rows.php,即:从手册$rows = $query->num_rows;- 以及受影响的行php.net/manual/en/mysqli.affected-rows.php -
是的,Fred -ii-,我从未注意到它有 ->affected_rows。请作为答案发布,我会在这里标记它