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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)