samedi 25 avril 2015

PHP/MySQLi: Commands out of sync; you can't run this command now


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

I have a mysqli connection, and have multiple queries to run on the page.

(ps - This is different from a similar-titled question, that attempts to open a second $mysqli->query() while the first is still active...in my case, I am closing one then opening a second)

Here is what I am doing:

<?php
$sql = 'CALL grc.spSelectProductAndVendor(1, 1)'; 
$rs = $conn->query($sql);
echo($conn->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 code.....

<?php
$sql = 'CALL grc.spSelectRecentDocumentScores();';
$rs = $conn->query($sql);
echo($conn->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
    }
}
?>

Question is, why does this error only occur upon the SECOND resultset call?

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

The first one works fine. The second does not. But I am closing the first $rs.

Any thoughts?

Thank you.....


Aucun commentaire:

Enregistrer un commentaire