MYSQLi - 命令不同步错误

时间:2014-04-27 14:10:16

标签: php mysqli

这里有一些代码。多年来一直坚持下去,我似乎无法解决错误。

    <?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;
    }

}

基本上每次运行代码时,它都会出现错误&#34;命令不同步;你现在不能运行这个命令&#34;。我花了两天时间尝试修复它 - 遵循人们关于在运行下一个结果之前释放结果的说明。我甚至在之前的代码中使用了一个准备好的语句,但它也没有用 - 这是新编写的代码,试图让它工作。我所做的所有阅读都表明,当您尝试运行sql命令而另一个仍在接收数据时会发生此错误 - 但是我已经调用了我的第一个查询,将其全部存储在一个数组中 - 然后我在数组中循环以获取下一批数据......并且这给了我一个错误,这就是我感到困惑的地方。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

感谢@andrewsi的帮助 - 事实证明,SELECT * FROM stats WHERE collated = 0", MYSQLI_USE_RESULT内的 MYSQLI_USE_RESULT 给了我错误。删除它允许我正常执行我的代码。

希望这有助于其他可能遇到同样问题的人。 =)