Jump to content

"commands Out Of Sync . . " Mysqli Error


cutielou22

Recommended Posts

I been trying to figure this out for about 3 or so hours (usually I wait 3 days of trying to fix something before I post here, but I can't wait xP) . . . and I am sure - as always - someone sees a simple fix and saves the day on PHPfreaks. :)

 

Below is the code and details of what is happening is below.

 

$stmt = $mysqli->prepare("SELECT question, uniqueid FROM pollquestion WHERE status = 1");
$stmt->execute();
$stmt->bind_result($question, $uniqueid);

while ($stmt->fetch())
{
echo "<center><div class=tablebox><h5>$question</h5></div></center><br><div class=tablebox>";

if (!$mysqli->query("SELECT optionname FROM pollchoices WHERE pollid = $uniqueid")) {echo "Multi-INSERT failed: (" . $mysqli->errno . ") " . $mysqli->error;} else{
$stmt2 = $mysqli->prepare("SELECT optionname FROM pollchoices WHERE pollid = ?");
$stmt2->bind_param('s', $uniqueid);
mysqli_free_result();
$stmt2->execute();
$stmt2->bind_result($option);
while ($stmt2->fetch()){echo "$option";}
$stmt2->close();
}
echo "</div>";
}
$stmt->close();

 

With this I get the error "Multi-INSERT failed: (2014) Commands out of sync; you can't run this command now." I looked it up on Google and it was saying I should use multi_query. I replaced the query with multi_query and still no luck and no change in error. So I changed it back.

 

I do stuff like this a lot in my coding and never came across this error. Should I be using multi_query? I never used it before, if so - how should I be using it?

 

This is what I am trying to get to happen: I get the question for the poll and the unique id. With the unique id I can find out what poll options goes with it. That is all I want to do and I cannot get the poll options to show.

Link to comment
Share on other sites

When you use only $stmt->execute() and $stmt->fetch() you have you fetch all the results before you can run any other queries. That's what is out of sync: running a second query while the first query isn't done sending data.

 

Fortunately all you have to do is buffer the results. Even more fortunately you don't have to do it yourself: call $stmt's store_result after you execute the query.

Edited by requinix
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.