如何维护多个打开的查询?

时间:2017-04-07 08:12:49

标签: php mysql

我有一些需要使用SQL的php文件。在那个SQL中我得到了多个结果,我在这里使用了一个while($stmt->fetch()){}循环,我需要在其中使用另一个SQL。这可以完成,还是我需要存储第一个SQL查询的结果,关闭后我可以打开新的SQL查询。

这是代码:

function compute_production($local_id, $GameID) {
    global $mysqli, $M, $Q;
    $sql = "SELECT `x`, `y`, `building`, `tier` FROM `GOD_battlefields` WHERE `owner`=? AND `game_id`=?";
    $stmt = $mysqli->prepare($sql);                                      
    $stmt->bind_param("ii", $local_id, $GameID);
    $stmt->execute();
    $stmt->bind_result($x, $y, $BUILDING, $TIER);
    while($stmt->fetch()) {

        $AB_triggered = array();

        fOReaCh(tech_get_value($BUILDING, "abilities") as $ability_name => $required_tier) {
            if ($TIER >= $required_tier) {


                switch($ability_name) {
                    case "auto_production":
                    $AB_triggered[$ability_name] = "true";
                        break;
                    case "toggle_production":
                    case "double_production":
                        // check if the order is clicked

                        $sql = "SELECT `post_value` FROM `GOD_cache` WHERE `post_name`=? AND `post_value` LIKE ? AND `game_id`=? AND `round`=?";
                        $AB_triggered[$ability_name] = "false";
                        $post_name = 'AbilityOrder_'.$x.'_'.$y;
                        $stmt2 = $mysqli->prepare($sql);                                      
                        $stmt2->bind_param("ssii", $post_name, $ability_name, $GameID, $Q->game_round);
                        $stmt2->execute();
                        $stmt2->bind_result($AbilityOrder);
                        if ($stmt2->fetch()) {
                            $stmt2->close();

                            $AB_triggered[$ability_name] = "true";
                        } else {
                            // Keep calm and do nothing
                            // Everything is fine
                            // No action needed
                            // Really


                        }
                        break;
                }
            }
        }



        foreach(tech_get_value($BUILDING, "production") as $r => $value) {
            if ($r == "s" || $r == "io" || $r == "w") {
                // check if cell contains those resources 
                $multiplier = ($Q->resources_in_cell($x, $y)[$r] > $value ? 1 : 0.15);
                $value *= $multiplier; // Multiply gained resources --> if mines/forests/quarries are empty, gained resources are decreased
            }
            $value *= tech_get_value($BUILDING, "productionm", $r) ** ($TIER - 1);
            if ($AB_triggered["toggle_production"] == "true" || $AB_triggered["auto_production"] == "true") {
                $RES_PER_TURN[$r] += $value;
            }
        }
        // information about production costs
        $HTML_battlefield .= "for the cost of: <br />";
        foreach(resources_for_production_gen($x, $y, array($BUILDING, $TIER)) as $resource => $cost) {
            if ($AB_triggered["toggle_production"] == "true" || $AB_triggered["auto_production"] == "true") {
                $RES_PER_TURN[array_search($resource, $dictionary_resource)] -= $cost;
            }
        }

    }

    return $RES_PER_TURN;
}

继续在$stmt2->bind_param();

上抛出错误

1 个答案:

答案 0 :(得分:1)

答案是:没有办法,如何维护多个打开的查询。

解决方案可能是使用某些INNER JOIN或其他JOIN或存储来自第一个SQL的信息,关闭SQL然后打开下一个SQL并使用存储的信息。