-2

I'm making a PHP/MySQL(i) website application, and I am getting this dreaded error, "Commands out of sync; you can't run this command now".

I have a connection to a database.

(P.S. - This is different from all other questions with similar titles -- I have gone through them all. What I want to do is OPEN first query, CLOSE first query, then OPEN second query. I do not want to execute multiple statements at once, or open multiple result-sets simultaneously)

Here is what I am doing:

<?php
$sql = 'CALL spSelectProductAndVendor(1, 1)'; 
$rs = $mysqli->query($sql);
echo($mysqli->error);
if ($rs->num_rows > 0) {
    while ($row=$rs->fetch_assoc()) {
        echo '<h1 class="page-header">' . $row["productname"] . '<small> pertaining to vendor: ' . $row["vendorname"] . '</small></h1>';
    }   
}   
$rs->close();
?>

HTML/CSS

<?php
$sql = 'CALL spSelectRecentDocumentScores();';
$rs = $mysqli->query($sql);
echo($mysqli->error);
if ($rs->num_rows > 0) {
    while ($row=$rs->fetch_assoc()) {
        if($row["totalwarnings"]>50) { echo '<tr class="danger">'; } else { echo '<tr class="active">'; }
?>
<td><a href="documentdetail.php?documentid=<?php echo $row["documentid"]; ?>" style="color:#000000"><?php echo $row["documentid"]; ?></a></td>
<td>etc...</td>
</tr>
<?php
    }
}
?>

Why does this error only occur upon the SECOND call?

$rs = $mysqli->query($sql);

The first one works fine. The second does not. But I am closing the first $rs. So what is missing?

  • in second this line should be – Saty Apr 25 '15 at 08:29
  • 1
    possible duplicate of [Commands out of sync; you can't run this command now](http://stackoverflow.com/questions/614671/commands-out-of-sync-you-cant-run-this-command-now) – bcmcfc Apr 25 '15 at 08:30
  • I looked at that before posting, but it is not a duplicate because that question runs two mysqli resultsets simultaneously, and the answer is to not have two resultsets open at the same time. With this example, I am opening one, then closing one, THEN opening #two. – Jason Wisdom Apr 25 '15 at 08:43
  • saty- I should just echo the whole line....as soon as the error is cleared up, I will echo the line rather than echoing the data-driven value. Thanks for the suggestion. – Jason Wisdom Apr 25 '15 at 08:44
  • can we see your function? – nomistic Apr 25 '15 at 16:29
  • There is no function. It is just PHP scripts embedded into an HTML/CSS page. I do have several include files but there are no OO functions. – Jason Wisdom Apr 25 '15 at 18:08

1 Answers1

0

Figured it out.. need to add $conn->next_result(); after the first resultset is closed:

$rs->close();
$mysqli->next_result();

Then the second $rs may be safely opened.

  • 1
    maybe instead of answering your own duplicate question, you should delete your question. – BRBT Apr 25 '15 at 16:44
  • Actually...the line $mysqli->next_result() was missing from the question, and that was the answer I had been seeking. so that is why it was placed in the answer. This may be helpful to somebody else who has the same issue. Also, this is not my own duplicate question - ??? – Jason Wisdom Apr 25 '15 at 18:06