执行成功,但num_rows返回0 [PHP-MySQL] [英] Execute Success but num_rows return 0 [PHP-MySQL]
问题描述
我刚遇到的问题是
$update_stmt->execute()
正常,并且数据库中的数据已更新
但是,$update_resultrow = $update_stmt->num_rows;
返回0吗?
我试图复制MySQL命令以在查询中运行,它也运行良好,如下所示:
I tried to copy MySQL command to run in query and it also worked well, like this:
UPDATE ACCOUNT_EMPLOYEE SET NAME = 'cccccc' WHERE ID = 1
问题代码在这里:
Problem's Code here:
$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();
}
}
}
}
推荐答案
是的,Fred -ii-,我从没注意到它有-> affected_rows.请张贴为答案,我会在这里标记出来
根据OP的要求.
看到这里的目标是测试查询是否确实成功,您需要使用affected_rows
.
Seeing that the goal here is to test if the query was indeed successful, you need to use affected_rows
.
按照手册 http://php.net/manual/en/mysqli.affected-rows .php
printf(受影响的行(UPDATE):%d \ n",$ mysqli-> affected_rows);
printf("Affected rows (UPDATE): %d\n", $mysqli->affected_rows);
- 面向对象的样式
int $ mysqli-> affected_rows;
int $mysqli->affected_rows;
旁注:
Sidenote:
使用
$update_resultrow = $update_stmt->num_rows;
并检查错误,将抛出一个错误,而不是返回0".
and checking for errors, would have thrown an error, rather than "return 0".
这篇关于执行成功,但num_rows返回0 [PHP-MySQL]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!