mysqli_affected_rows return -1 但更新查询成功

mysqli_affected_rows return -1 but update query is successful

我有一个页面 php 代码获取用户的新密码字符串来更改用户密码,并在一些验证代码后发送到处理页面以在用户 table 中更新记录。在将新密码字符串发送到处理页面并执行更新查询后,mysqli_affected_rows return -1 但更新查询成功并且密码已更改或者如果以前的密码字符串与以前的密码相同,则更新查询不会进行任何更改,但仍然 returns -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");
     }
 }

我在 Whosebug 和 google 中发现了一些关于此的问题,并与 xdebug 讨论了 mysqli 中的错误 https://bugs.php.net/bug.php?id=67348 但我不使用 $connect ->stat 和一些页面写了关于 mysqli 中每个启用 xdebug 的错误,所以我在 php.ini 中禁用 xdebug,但在所有状态和事件以及所有位置中禁用 mysqli_affected_rows return -1还。

我之前用GoogleChrome调试phpstorme

我应该在 Chrome 中禁用某些东西吗?

更新:(2018/08/01):

几天在Google各种网站上搜索并在Whosebug中搜索并按照所写的步骤和建议进行搜索后,我无法解决。

一个叫"siddharaj solanki"的人在这个问题中写道:

写道:

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.

所以我认为根据我的研究和这个 link:

这个函数不会轻易解决这个错误

https://www.google.com/search?q=bug+mysqli_affected_rows

请指导我什么是解决方案或什么是好的或最好的方法而不是 mysqli_affected_rows 检查更新查询?

好时光

请帮我解决一下。

谢谢。

上面的评论确实解释了问题所在,但我将在这里进行总结并将其标记为社区维基答案。

您的代码创建了一个到数据库的连接,但是您说您的 db_query_func() 函数创建了一个单独的数据库连接到 运行 查询。

受影响的行计数只能报告同一连接中受查询影响的行。但是您在不同的连接中执行了更新。

考虑这个类比:您打开两个单独的 ssh 会话到远程服务器。你运行一个命令在一个shell,然后在其他shell你用!!重复这个命令。第二个 shell 不知道第一个 shell 的命令历史,所以它不能重复相同的命令。

您需要使用相同的数据库连接来执行查询,然后报告受影响的行。也许上面的评论没有说清楚,但你可以通过将连接传递给你的查询函数来解决这个问题:

 $result = db_query_func($link_con, $query);

在该函数中,不要创建新的 mysqli 连接,而是使用您作为参数传递的连接。

然后当它 return 时,它具有正确的上下文,因此您可以从同一连接获取 mysqli_affected_rows($link_con)

还有其他可能的替代解决方案:

  • 根本不清楚为什么需要 db_query_func()。也许您应该从主代码中调用 mysqli_query()

  • 否则 db_query_func() 应该调用 mysqli_affected_rows() 本身,并且 return 值。