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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。