gpt4 book ai didi

php - mysqli_affected_rows 返回-1 但更新查询成功

转载 作者:行者123 更新时间:2023-12-05 04:08:02 25 4
gpt4 key购买 nike

我有一个 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 中禁用某些功能吗?

更新:(2018/08/01):

用谷歌在各个网站上搜索了几天并在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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com