如何解决“命令不同步”错误?

时间:2018-08-28 09:07:58

标签: php mysqli

我有以下代码片段,我尝试执行两个Mysqli准备的语句。不幸的是,我只能获得第一个准备好的语句的结果集。我试图通过回显识别错误的出处来对此进行故障排除:

 $noError = mysqli_stmt_execute($stmt2);

 if(!$noError)
  {
    echo "Error:<br/>" . mysqli_error($con);
  }

我得到了: 错误: 命令不同步;您不能立即运行此命令

代码段:-

 $stmt = mysqli_stmt_init($con);
 $stmt2 = mysqli_stmt_init($con);

 mysqli_stmt_prepare($stmt,'select fname,city from member where mid=?');
 mysqli_stmt_prepare($stmt2,'select paying_date,amount from fees where 

mid=?');

 mysqli_stmt_bind_param($stmt,'i',$MID);
 mysqli_stmt_bind_param($stmt2,'i',$MID);

 for($i=1;$i<=6;$i++)
 {
  $MID = $i;


  mysqli_stmt_execute($stmt);
  mysqli_stmt_execute($stmt2);



  mysqli_stmt_bind_result($stmt,$fname,$city);
  mysqli_stmt_bind_result($stmt2,$paying_date,$amount);



  mysqli_stmt_fetch($stmt);
  mysqli_stmt_fetch($stmt2);

  echo $fname." ".$city." ".$paying_date." ".$amount."<br/>"; 
 }

那么,如何摆脱这个错误?预先感谢。

1 个答案:

答案 0 :(得分:2)

最后,经过更多研究后,我自己得到了答案,但可悲的是,有些 疯狂 的人不愿提供任何线索或暗示,而只是投票问题。因此,我决定回答自己。代码 snippet 现在变为:

 $stmt = mysqli_stmt_init($con);
 $stmt2 = mysqli_stmt_init($con);

 mysqli_stmt_prepare($stmt,'select fname,city from member where mid=?');
 mysqli_stmt_prepare($stmt2,'select paying_date,amount from fees where 

mid=?');

 mysqli_stmt_bind_param($stmt,'i',$MID);
 mysqli_stmt_bind_param($stmt2,'i',$MID);

 for($i=1;$i<=6;$i++)
 {
  $MID = $i;

  mysqli_stmt_execute($stmt);
  mysqli_stmt_store_result($stmt);
  mysqli_stmt_bind_result($stmt,$fname,$city);
  mysqli_stmt_fetch($stmt);

  mysqli_stmt_execute($stmt2);
  mysqli_stmt_bind_result($stmt2,$paying_date,$amount);
  mysqli_stmt_fetch($stmt2);
  echo $fname." ".$city." ".$paying_date." ".$amount."<br/>";

 }