简体   繁体   中英

MYSQLi - Commands out of sync error

Got some code here. Been stuck on it for ages and I can't seem to get around the error.

    <?PHP

error_reporting(E_ALL);
ini_set('display_errors',1);

$mysqli = new mysqli('localhost', 'username', 'password', 'table');
$statsObjects = array();
$collatedObjects = array();

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

Global $areRows;
$areRows = 2;




if( $result = $mysqli->query("SELECT * FROM stats WHERE collated = 0", MYSQLI_USE_RESULT) )
{
    while($row = $result->fetch_assoc())
    {
        array_push($statsObjects, 
            new Statistic(
                $row['ID'],
                $row['player_GUID'],
                $row['shots_fired'],
                $row['shots_hit'],
                $row['damage_done'],
                $row['damage_taken'],
                $row['friendly_damage_done'],
                $row['friendly_damage_taken'] 
                ));
    }
 $success = true;

} //end if

$result->free_result();
if($success)
{
    foreach($statsObjects as $stat)
    {
        $statsGuid = $stat->getGuid();
        $query = "SELECT COUNT(*) AS total FROM collatedStats WHERE player_GUID = '" . $statsGuid . "'";
        if( $result2 = $mysqli->query($query, MYSQLI_USE_RESULT) )
        {
            $value = $result2->fetch_assoc();
        $rows = $value['total'];
        if($rows > 0)
        {
            $areRows = 1;
        }
        else
        {
            $areRows = 0;
        }
        }
        else 
        {
            echo("Error <br/>");
            echo($mysqli->error);
        }

        if($areRows == 1)
        {
            echo("Found a row! <br/>");
        }
        elseif($areRows == 0)
        {
            Echo("No rows found. =) <br/>");
        }
    } //end foreach
}


//OBJECT
class Statistic
{
    var $GUID;
    var $shotsfired;
    var $shotshit;
    var $damagedone;
    var $damagetaken;
    var $friendlydamagedone;
    var $friendlydamagetaken;
    var $ID;

    function Statistic($ID, $GUID, $fired, $hit, $ddone, $dtaken, $fddone, $fdtaken)
    {
        $this->id = $ID;
        $this->GUID = $GUID;
        $this->shotsfired = $fired;
        $this->shotshit = $hit;
        $this->damagedone = $ddone;
        $this->damagetake = $dtaken;
        $this->friendlydamagedone = $fddone;
        $this->friendlydamagetaken = $fdtaken;
    }

    function getID()
    {
        return $this->ID;
    }

    function getGuid()
    {
        return $this->GUID;
    }

    function getShotsFired()
    {
        return $this->shotsfired;
    }

    function getShotsHit()
    {
        return $this->shotshit;
    }

    function getDamageDone()
    {
        return $this->damagedone;
    }

    function getDamageTaken()
    {
        return $this->damagetaken;
    }

    function getFriendlyDDone()
    {
        return $this->friendlydamagedone;
    }

    function getFriendlyDTaken()
    {
        return $this->friendlydamagetaken;
    }

    function getAccuracy()
    {
        if($shotsfired == 0)
        {
            $accuracy = 0;
        }
        else
        {
            $accuracydec = $shotshit / $shotsfired;
            $accuracy = $accuracydec * 100;
        }

        return $accuracy;
    }

}

Basically every time i run the code, it keeps coming up with the error "Commands out of sync; you can't run this command now". I've spent 2 days trying to fix it - following peoples instructions about freeing the result before running the next one. I even used a prepared statement in previous code however it didn't work either - this is newly written code in an attempt to get it working. All the reading i've done suggests that this error happens when you try to run an sql command while another one is still receiving data - however i've called my first query, stored it all in an array - and then i'm looping through the array to get the next lot of data..and that's giving me an error, which is where i'm getting confused.

Any help would be appreciated!

Thank You to @andrewsi for his help - it turns out that having the MYSQLI_USE_RESULT inside SELECT * FROM stats WHERE collated = 0", MYSQLI_USE_RESULT was giving me the error. Removing that allowed me to do my code normally.

Hopefully this helps others that may have the same problem. =)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM