dongwaner1367 2015-01-01 13:30
浏览 56
已采纳

MySQLi命令不同步,具有简单的query()函数

I can't figure out why I'm getting the Commands out of sync; you can't run this command now error. I can't seem to find a way to fix this.

As you can probably see from the code, I need to execute TWO SQL statements. One to check if the password is correct, and the other, which runs only if the previous one returns true, to store all user data in session variables.

Here is my code:

$check_pw_query = $conn->query("CALL checkPassword('{$username}', '{$password}')");
$check_pw_fetched = $check_pw_query->fetch_assoc();
$check_pw_query->close();

if($check_pw_fetched['password_correct']) {

    unset($check_pw_fetched);

    if(!$get_user_data = $conn->query("CALL getUserData('{$username}')")/*->fetch_assoc()*/) {
        echo $conn->error;
    }

    $_SESSION["user_username"] = $username;
    $_SESSION["user_id"] = $get_user_data["id"];
    $_SESSION["user_name"] = $get_user_data["name"];
    $_SESSION["user_email"] = $get_user_data["email"];
    $_SESSION["user_type"] = $get_user_data["type"];

    $conn->close();
    unset($conn);
    send_home(false);

}

Help is much appreciated.

Thank you

  • 写回答

1条回答 默认 最新

  • duanbimo7212 2015-01-02 11:57
    关注

    Ok, as there were no real helpfully answers, I'll answer it myself, to hopefully help someone else with the same problem.

    The problem is that, whenever you call a stored procedure, it will output one more result that defined anywhere. So if your procedure returns 1 value, php will get 2. The last one is only there to tell you that there are no more values. BUT you have to deal with that one as well to free the connection ($conn in my example).

    So, that you do is, simply, add this to your code, right after the $check_pw_query->close(); :

    $conn->next_result();

    And there you have it. It will now work perfectly fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大