How to resolve “Commands out of sync” Error?
How to resolve “Commands out of sync” Error?
I have the following code snippet where I try to execute two Mysqli prepared statements. Unfortunately, I can get only the result set of the first prepared statement. I tried to troubleshoot this by identifying where the error is coming from by echoing:
$noError = mysqli_stmt_execute($stmt2);
if(!$noError)
echo "Error:<br/>" . mysqli_error($con);
and I got this:
Error:
Commands out of sync; you can't run this command now
The code 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_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/>";
So, how do I get rid of this error? With thanks in advance.
1 Answer
1
Finally, after some more research I got the answer on my own, but sadly, there are some insane people who instead of providing any clue or hint, just downvote the question. So, I decided to answer myself. The code snippet now becomes:
$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/>";
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.