PHP 命令不同步错误

2022-08-30 15:42:57

我在PHP / MySQLi中使用两个预准备语句从mysql数据库中检索数据。但是,当我运行语句时,我得到“命令不同步,您现在无法运行命令”错误。

这是我的代码:

    $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();

我尝试过:

  • 将预准备语句移动到两个单独的对象。
  • 使用代码:

    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:“不同步”错误来自第二个语句的“$stmt 1->错误”


答案 1

在 mysqli::query 中,如果您使用 MYSQLI_USE_RESULT则所有后续调用都将返回错误 命令不同步,除非您调用 mysqli_free_result()

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

<?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();
?>

我希望这会有所帮助


答案 2

“命令不同步;您现在无法运行此命令”

有关此错误的详细信息可以在mysql文档中找到。阅读这些详细信息可以清楚地看出,在同一连接上执行另一个预准备语句之前,需要完全获取预准备语句执行的结果集。

可以通过使用存储结果调用来解决此问题。以下是我最初尝试执行的操作的示例:

<?php

  $db_connection = new mysqli('127.0.0.1', 'user', '', 'test');

  $post_stmt = $db_connection->prepare("select id, title from post where id = 1000");
  $comment_stmt = $db_connection->prepare("select user_id from comment where post_id = ?");

  if ($post_stmt->execute())
  {
    $post_stmt->bind_result($post_id, $post_title);

    if ($post_stmt->fetch())
    {
      $comments = array();

      $comment_stmt->bind_param('i', $post_id);
      if ($comment_stmt->execute())
      {
        $comment_stmt->bind_result($user_id);
        while ($comment_stmt->fetch())
        {
          array_push($comments, array('user_id' => $user_id));
        }
      }
      else
      {
        printf("Comment statement error: %s\n", $comment_stmt->error);
      }
    }
  }
  else
  {
    printf("Post statement error: %s\n", $post_stmt->error);
  }

  $post_stmt->close();
  $comment_stmt->close();

  $db_connection->close();

  printf("ID: %d -> %s\n", $post_id, $post_title);
  print_r($comments);
?>

上述情况将导致以下错误:

注释语句错误:命令不同步;您现在无法运行此命令

PHP 注意:未定义的变量:post_title错误.php在第 41 行 ID:9033 -> 数组 ( )

以下是使其正常工作所需的操作:

<?php

  $db_connection = new mysqli('127.0.0.1', 'user', '', 'test');

  $post_stmt = $db_connection->prepare("select id, title from post where id = 1000");
  $comment_stmt = $db_connection->prepare("select user_id from comment where post_id = ?");

  if ($post_stmt->execute())
  {
    $post_stmt->store_result();
    $post_stmt->bind_result($post_id, $post_title);

    if ($post_stmt->fetch())
    {
      $comments = array();

      $comment_stmt->bind_param('i', $post_id);
      if ($comment_stmt->execute())
      {
        $comment_stmt->bind_result($user_id);
        while ($comment_stmt->fetch())
        {
          array_push($comments, array('user_id' => $user_id));
        }
      }
      else
      {
        printf("Comment statement error: %s\n", $comment_stmt->error);
      }
    }

    $post_stmt->free_result();
  }
  else
  {
    printf("Post statement error: %s\n", $post_stmt->error);
  }

  $post_stmt->close();
  $comment_stmt->close();

  $db_connection->close();

  printf("ID: %d -> %s\n", $post_id, $post_title);
  print_r($comments);
?>

关于上面的示例,需要注意的几件事:

The bind and fetch on the statement still works correctly.
Make sure the results are freed when the processing is done.

推荐