- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 php 代码页面,使用用户的新密码字符串来更改用户密码,并在一些验证代码后发送到处理页面以更新用户表中的记录。将新密码字符串发送到处理页面并执行更新查询后,mysqli_affected_rows 返回 -1 但更新查询成功且密码已更改或者如果以前的密码字符串与以前的密码相同,更新查询不进行任何更改但仍返回 -1 .
我在所有代码中都使用 mysqli 函数。db_conncet_fnc()、db_query_func()、db_fetch_assoc_func()、...这些函数包含相同的 mysqli 函数。
我的进程页面代码是这样的:
$link_con=db_conncet_fnc();
require_once("PassHash.php");
$query = "SELECT * FROM mdr WHERE m_id='$md_id'";
$result = db_query_func($query);
$affecteds= mysqli_affected_rows($link_con);
$_SESSION["affecteds"]=$affecteds; //this is for test before UPDATE query
$_SESSION["affected-s-oop"]=$link_con->affected_rows; //this is for test before UPDATE query
$rec = db_fetch_assoc_func($result);
$p_hasher = new PassHsh(25, FALSE);
$ans = "2"; //answer without error is 2
if($rec)
{
if ($pass != "" && $newpsw != "" && $check = $p_hasher->checkpss($newpsw, $rec["userpass"])) {
if ($chngpsw_err < 2) {
$_SESSION["chngpsw_err"] = "has";
}
$_SESSION["pass_nwpass_is_equal"] = "yes";
header("location:index.php?page=chngpass");
exit();
} elseif ($check = $p_hasher->checkpss($pass, $rec["userpass"])) {
$hashed_psw = $p_hasher->HashPassword($newpsw);
$query = "UPDATE `mdr` SET `userpass`='$hashed_psw' WHERE m_id='" . $md_id . "' ";
$result = db_query_func($query);
$affect_upd = mysqli_affected_rows($link_con);
$_SESSION["affect_upd"] = $affect_upd; //by function
$_SESSION["affect_upd-oop"] = $link_con->affected_rows; //by object
if ($affect_upd == 0) {
$_SESSION["update_result"] = "err0";
header("location: index.php?page=chngpass");
exit();
}
if ($affect_upd == -1) {
$_SESSION["update_result"] = "err-1";
header("location: index.php?page=chngpass");
exit();
}
if ($affect_upd > 0) {
$_SESSION["update_result"] = "ok";
header("location: index.php?page=chngpass");
exit();
}
} else {
$ans = "1";
header("location: index.php?page=chngpass&ans=$ans");
}
}
我在 stackoverflow 和 google 中发现了一些关于此的问题,并使用 xdebug 讨论了 mysqli 中的错误,就像这样 https://bugs.php.net/bug.php?id=67348但是我不使用 $connect->stat 并且一些页面写了关于 mysqli 中每个启用 xdebug 的错误,所以我在 php.ini 中禁用 xdebug 但 mysqli_affected_rows 在所有状态和事件以及所有位置中返回 -1。
我之前使用 Google Chrome 来调试 phpstorme。
我应该在 Chrome 中禁用某些功能吗?
用谷歌在各个网站上搜索了几天并在stackoverflow中搜索并按照写的步骤和建议进行了几天后,我无法解决。
一个叫“siddharaj solanki”的人在这个问题中写道:
mysqli_affected_rows creates new connection (read details) 写道:
Now the problem is I don't understand how to pass database link ($link) in mysqli_affected_rows() function. I tried above, but it seems to create a new database connection, so mysqli_affected_rows returns 0 instead of 1.
因此,根据我的研究和此链接,我认为此功能的错误不会轻易解决:
https://www.google.com/search?q=bug+mysqli_affected_rows
请指导我什么是解决方案或什么是好的或最好的方法而不是 mysqli_affected_rows 来检查更新查询?
好时光
请帮我解决这个问题。
谢谢。
最佳答案
上面的评论确实解释了问题所在,但我会在这里进行总结并将其标记为社区维基答案。
您的代码创建了一个到数据库的连接,但是您说您的 db_query_func()
函数创建了一个单独的数据库连接来运行查询。
affected-rows count 只能报告在同一连接中受查询影响的行。但是您在不同的连接中执行了更新。
考虑这个类比:您打开两个单独的 ssh
session 到远程服务器。您在一个 shell 中运行命令,然后在 其他 shell 中使用 !!
重复该命令。第二个 shell 不知道第一个 shell 的命令历史,因此它不能重复相同的命令。
您需要使用相同的数据库连接来执行查询,然后报告受影响的行。也许上面的评论没有说清楚,但你可以通过将连接传递给你的查询函数来解决这个问题:
$result = db_query_func($link_con, $query);
在该函数中,不要创建新的 mysqli 连接,而是使用您作为参数传递的连接。
然后当它返回时,它具有正确的上下文,因此您可以从同一连接获取 mysqli_affected_rows($link_con)
。
还有其他可能的替代解决方案:
根本不清楚为什么需要 db_query_func()
。也许您应该从主代码中调用 mysqli_query()
。
否则 db_query_func()
应该调用 mysqli_affected_rows()
本身,并返回值。
关于php - mysqli_affected_rows 返回-1 但更新查询成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48131001/
我想用 2 个 mysql 列之间的差异结果更新一列,并计算有多少行受到影响。在我的例子中它只能是1。这是我正在使用的mysql查询,它根本不一致 $connection->query("UPDATE
在我的开发环境中,对 mysqli_affected_rows($link) 的所有调用都意外返回 -1,表明出现某种错误。 从 SQL 控制台执行的相同 SQL 按预期工作。 为了避免人们试图理解代
我有这个代码: if(mail($to, $subject, $message, $headers)){ $insert_member_sql = "INSERT INTO members (
我正在编写一个函数来使用以下代码删除数据库中的一个用户,mysqli_affected_rows 怎么总是返回 0? $query = "DELETE FROM taxaapp_users WHERE
(抱歉,我真的不知道我在做什么。) 我在 Facebook 的 iframe 中有这个 Unity 游戏,它调用同一目录中的一个 php 文件,这很有效。我想让它做的是更新播放器记录(如果存在),如果
我在 mysqli_affected_rows 上遇到了一些问题,它总是返回 1,即使更新没有受到影响。 $dbConnect = mysql_connect($dbHost, $dbUser, $d
我有一个 php 代码页面,使用用户的新密码字符串来更改用户密码,并在一些验证代码后发送到处理页面以更新用户表中的记录。将新密码字符串发送到处理页面并执行更新查询后,mysqli_affected_r
我所有的 mysql 函数似乎都有这个问题。简单的插入语句有效,但当我检查受影响的行时,它返回 -1。 这行得通,一个新行被插入到数据库中,但是调试器将 mysqli_affected_rows 显示
PHP docs for mysqli_num_rows说 Returns the number of rows in the result set. PHP docs for mysqli_affe
假设我有 MySQL 查询来更新一行: $res = mysqli_query($link, "UPDATE table SET val=1 WHERE id=5"); if($res) {
我正在为表单进行服务器端验证。使用 AJAX,表单将“用户名”输入字段中的值发送到我的 PHP 页面,然后检查该用户名是否已存在于数据库中。 这是我的 PHP 代码: $result = mysqli
我是一名优秀的程序员,十分优秀!