duanbei6427 2017-04-07 08:12
浏览 11
已采纳

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

I have some php file which needs to use SQL. In that SQL I get multiple results and I use here a while($stmt->fetch()){} loop inside which I need to use another SQL. Can this be accomplished or do I need to store result of the first SQL query and after closing it I can open new SQL query.

Here's code:

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;
}

Keeps throwing errors on the $stmt2->bind_param();

  • 写回答

1条回答 默认 最新

  • dpub33855 2017-04-09 20:57
    关注

    The answer is: there is no way, how to maintain multiple open queries.

    Solution may be using some INNER JOIN, or other JOIN or storing the information from first SQL, close the SQL and then open the next SQL and use stored information.

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

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀