dtttlua7165 2014-04-27 14:10
浏览 35
已采纳

MYSQLi - 命令不同步错误

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
", $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!

  • 写回答

1条回答 默认 最新

  • dousao2186 2014-04-28 02:37
    关注

    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. =)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化