繁体   English   中英

为什么MySQLi multi_query显示“命令不同步; 您不能立即运行此命令”错误

[英]Why is MySQLi multi_query showing a “Commands out of sync; you can't run this command now” error

我正在尝试将0-100的分数转换为#1,#2,#3,...#n基准排名。 为此,我计划了以下查询:

$q= "SELECT @rownum:=0;";
$q.=" INSERT INTO ranks (`uid`, `rank`, `sample_date`) (SELECT user_id, @rownum:=@rownum+1, NOW() FROM `scores` WHERE 1 ORDER BY score DESC)";

这在SQL控制台(phpmyadmin)中运行良好,但是当尝试通过PHP的MySQLi运行时,使用其multi_query运行以下错误:

Commands out of sync; you can't run this command now

我的multi_query包装器:(在扩展mysqli的类中)

public function multiQuery($query) {
    if (@parent::multi_query($query)) {
        $i = 0; 
        do { 
            $i++; 
        } while (@parent::next_result()); 
    }
    if (!$result) {
        printf("MySQLi error:<br><b>%s</b><br>%s <br>", $this->error, $query);
    }

    return $result;
}

为什么会出现该错误?

必须先使用mysqli_store_result()mysqli::use_result()然后才能检索查询结果或移至下一个结果。 我相信您正在寻找这样的东西:

public function multiQuery($query)
{
    if (@parent::multi_query($query))
    {
        do
        {
            if ($result = @parent::use_result())
            {
                return $result;
                // this will end the function on the first result encountered.
                // If that is not what you desire, you could create an array of results,
                // and return it after the do-while loop finishes.
            }
            else
            {
                printf("MySQLi error:<br><b>%s</b><br>%s <br>", $this->error, $query);
                return NULL;  // or you could return the error encountered
            }
        } while (@parent::next_result()); 
    }
}

希望有帮助:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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