简体   繁体   中英

MySQLi Commands out of sync, with simple query() function

I can't figure out why I'm getting the Commands out of sync; you can't run this command now 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM