Php 如何维护多个打开的查询?
我有一些php文件需要使用SQL。在该SQL中,我得到了多个结果,我在这里使用了Php 如何维护多个打开的查询?,php,mysql,Php,Mysql,我有一些php文件需要使用SQL。在该SQL中,我得到了多个结果,我在这里使用了while($stmt->fetch()){}循环,在该循环中我需要使用另一个SQL。这可以实现吗?或者我需要存储第一个SQL查询的结果,然后在关闭它之后,我可以打开新的SQL查询 以下是代码: function compute_production($local_id, $GameID) { global $mysqli, $M, $Q; $sql = "SELECT `x`, `y`, `buil
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;
}
function compute\u production($local\u id,$GameID){
全局$mysqli,$M,$Q;
$sql=“从‘上帝战场’中选择‘x’、‘y’、‘建筑’、‘层’,其中‘所有者’=?和‘游戏id’=?”;
$stmt=$mysqli->prepare($sql);
$stmt->bind_param(“ii”,$local_id,$GameID);
$stmt->execute();
$stmt->bind_result($x,$y,$BUILDING,$TIER);
而($stmt->fetch()){
$AB_触发器=数组();
fOReaCh(技术获取价值($BUILDING,“abilities”)为$ability\u name=>$required\u tier){
如果($TIER>=$required\u TIER){
交换机($U名称){
案例“汽车生产”:
$AB_触发[$ability_name]=“真”;
打破
案例“切换生产”:
案例“双_生产”:
//检查订单是否已单击
$sql=“从'GOD\u cache'中选择'post\u value',其中'post\u name`=?和'post\u value`LIKE','game\u id`=?和'round`=?”;
$AB_触发[$ability_name]=“false”;
$post_name='AbilityOrder_.$x.'$y;
$stmt2=$mysqli->prepare($sql);
$stmt2->bind_参数(“ssii”、$post_name、$ability_name、$GameID、$Q->game_round);
$stmt2->execute();
$stmt2->bind_result($AbilityOrder);
如果($stmt2->fetch()){
$stmt2->close();
$AB_触发[$ability_name]=“真”;
}否则{
//保持冷静,什么也不做
//一切都很好
//不需要采取行动
//真的吗
}
打破
}
}
}
foreach(技术价值($BUILDING,“production”)为$r=>$value){
如果($r==“s”| |$r==“io”| |$r==“w”){
//检查单元格是否包含这些资源
$multiplier=($Q->resources\u在单元中($x,$y)[$r]>$value?1:0.15);
$value*=$multiplier;//增加获得的资源-->如果矿山/森林/采石场为空,则减少获得的资源
}
$value*=技术获取价值($BUILDING,“productionm”,$r)**($TIER-1);
如果($AB_-tricked[“toggle_-production”]==“true”| |$AB_-tricked[“auto_-production”]==“true”){
$RES_每回合[$r]+=$value;
}
}
//关于生产成本的信息
$HTML_battley.=“费用:
”;
foreach(资源用于生产生成($x,$y,阵列($BUILDING,$TIER))作为$resource=>$cost){
如果($AB_-tricked[“toggle_-production”]==“true”| |$AB_-tricked[“auto_-production”]==“true”){
$RES\u每回合[数组搜索($resource,$dictionary\u resource)]-=$cost;
}
}
}
每回合返回$RES_;
}
不断在
$stmt2->bind_param()上抛出错误代码>答案是:没有办法,如何维护多个打开的查询
解决方案可能是使用一些内部联接
,或其他联接
,或存储第一个SQL中的信息,关闭SQL,然后打开下一个SQL并使用存储的信息。不断在$stmt2->bind_param()上抛出什么错误代码>知道正在抛出哪些错误会有点有用。错误的类型并不重要,因为您不能有多个打开的SQL查询。通过存储第一个查询的结果并在关闭后打开下一个查询,我解决了这个问题。因此,我不是在($stmt->fetch())
的时候循环一个,而是在一个结果数组中循环。。。。