命令不同步;您现在不能运行这个命令;PHP While循环


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

当运行下面的代码时,在第二个循环(它循环次数的数量),它给我一个命令不同步错误。是什么导致了这种情况?

PHP代码:

while ($i < $quantity) {
    $result = mysqli_multi_query($con, "
        INSERT INTO parts (capacity, length, width, height, orientation, weight, location, description, status, partNumber) VALUES ('$capacity', '$length', '$width', '$height', '$orient', '$weight', '$location', '$desc', '$status', '$partNum');
        INSERT INTO craneparts (craneID, partsID) VALUES ('$craneID', LAST_INSERT_ID());
    ") or die(mysqli_error($con)); 
    $i++;
}

https://dev.mysql.com/doc/refman/5.0/en/commands-out-of-sync.html

如果你得到命令不同步;您现在不能运行这个命令你的客户端代码,你调用客户端函数的顺序不对。

例如,如果您正在使用mysql_use_result()和在调用mysql_free_result()之前尝试执行一个新的查询。如果尝试执行两个返回数据的查询,也可能发生这种情况不调用mysql_use_result()或mysql_store_result()

尝试在mysqli_multi_query调用之间调用mysqli_next_result

while ($i < $quantity) {
    $result = mysqli_multi_query($con, "
        INSERT INTO parts (capacity, length, width, height, orientation, weight, location, description, status, partNumber) VALUES ('$capacity', '$length', '$width', '$height', '$orient', '$weight', '$location', '$desc', '$status', '$partNum');
        INSERT INTO craneparts (craneID, partsID) VALUES ('$craneID', LAST_INSERT_ID());
    ") or die(mysqli_error($con));
    while(mysqli_more_results($con) && mysqli_next_result($con)) {;} // flush multi_queries
    $i++;
}