PHP 命令不同步错误 [英] PHP Commands Out of Sync error

查看:28
本文介绍了PHP 命令不同步错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 PHP/MySQLi 中使用两个准备好的语句从 mysql 数据库中检索数据.但是,当我运行这些语句时,出现命令不同步,您现在无法运行该命令"错误.

I am using two prepared statements in PHP/MySQLi to retrieve data from a mysql database. However, when I run the statements, I get the "Commands out of sync, you can't run the command now" error.

这是我的代码:

    $stmt = $mysqli->prepare("SELECT id, username, password, firstname, lastname, salt FROM members WHERE email = ? LIMIT 1";
    $stmt->bind_param('s', $loweredEmail);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($user_id, $username, $db_password, $firstname, $lastname, $salt);
    $stmt->fetch();

    $stmt->free_result();
    $stmt->close();

    while($mysqli->more_results()){
        $mysqli->next_result();
    }

    $stmt1 = $mysqli->prepare("SELECT privileges FROM delegations WHERE id = ? LIMIT 1");
    //This is where the error is generated
    $stmt1->bind_param('s', $user_id);
    $stmt1->execute();
    $stmt1->store_result();
    $stmt1->bind_result($privileges);
    $stmt1->fetch();

我尝试过的:

  • 将准备好的语句移到两个单独的对象中.
  • 使用代码:

  • Moving the prepared statements to two separate objects.
  • Using the code:

while($mysqli->more_results()){
    $mysqli->next_result();
}
//To make sure that no stray result data is left in buffer between the first
//and second statements

  • 使用 free_result() 和 mysqli_stmt->close()
  • PS: 'Out of Sync' 错误来自第二条语句的 '$stmt1->error'

    PS: The 'Out of Sync' error comes from the second statement's '$stmt1->error'

    推荐答案

    在 mysqli::query 如果您使用 MYSQLI_USE_RESULT 所有后续调用将返回错误 Commands out of sync 除非您调用 mysqli_free_result()

    In mysqli::query If you use MYSQLI_USE_RESULT all subsequent calls will return error Commands out of sync unless you call mysqli_free_result()

    在调用多个存储过程时,您可能会遇到以下错误:命令不同步;您现在无法运行此命令".即使在调用之间对结果对象使用 close() 函数时,也会发生这种情况.要解决此问题,请记住在每次调用存储过程后调用 mysqli 对象上的 next_result() 函数.请参见下面的示例:

    When calling multiple stored procedures, you can run into the following error: "Commands out of sync; you can't run this command now". This can happen even when using the close() function on the result object between calls. To fix the problem, remember to call the next_result() function on the mysqli object after each stored procedure call. See example below:

    <?php
    // New Connection
    $db = new mysqli('localhost','user','pass','database');
    
    // Check for errors
    if(mysqli_connect_errno()){
     echo mysqli_connect_error();
    }
    
    // 1st Query
    $result = $db->query("call getUsers()");
    if($result){
         // Cycle through results
        while ($row = $result->fetch_object()){
            $user_arr[] = $row;
        }
        // Free result set
        $result->close();
        $db->next_result();
    }
    
    // 2nd Query
    $result = $db->query("call getGroups()");
    if($result){
         // Cycle through results
        while ($row = $result->fetch_object()){
            $group_arr[] = $row;
        }
         // Free result set
         $result->close();
         $db->next_result();
    }
    else echo($db->error);
    
    // Close connection
    $db->close();
    ?>
    

    我希望这会有所帮助

    这篇关于PHP 命令不同步错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆