我有一个函数,用于 php 游戏,其中有一个 for 循环检查房间 ID。基本上,for 循环只运行一次,而它应该运行两次。下面的代码块说明了(第一个带有 $i 的 for 循环):
// successful sql login & $player is correctly set with '', and all below sql
// statements execute correctly. $count is 2
function setMonstersLocation($player,$rooms_id){
if (mysql_result(mysql_query("SELECT state FROM present WHERE player = $player AND items_id IS NULL AND monsters_id IS NULL AND quantity IS NULL AND rooms_id IS NOT NULL"),0) == 0){
$sql = mysql_query("SELECT COUNT(*) FROM present WHERE player = $player AND items_id IS NULL AND monsters_id > 100 AND monsters_id < 300");
$count = mysql_result($sql,0);
$sql = mysql_query("SELECT monsters_id FROM present WHERE player = $player AND items_id IS NULL AND monsters_id > 100 AND monsters_id < 300 ORDER BY monsters_id");
for ($i = 0; $i < $count; $i+= 1) {
$sql = mysql_query("SELECT monsters_id FROM present WHERE player = $player AND items_id IS NULL AND monsters_id > 100 AND monsters_id < 300 ORDER BY monsters_id");
$result = mysql_result($sql,$i);
if (mysql_result(mysql_query("SELECT COUNT(*) FROM present WHERE player = $player AND items_id IS NULL AND monsters_id=$result AND quantity = 1"),0) == 1){
$final = -1;
$array[1] = $rooms_id - 1;
$array[2] = $rooms_id + 1;
$array[3] = $rooms_id - 100;
$array[4] = $rooms_id + 100;
$array[5] = $rooms_id - 200;
$array[6] = $rooms_id - 101;
$array[7] = $rooms_id - 99;
$array[8] = $rooms_id - 2;
$array[9] = $rooms_id + 200;
$array[10] = $rooms_id + 101;
$array[11] = $rooms_id + 99;
$array[12] = $rooms_id + 2;
if ($result < 200){
for ($j = 1; $j < 5; $j+=1){
$sql = mysql_query("SELECT COUNT(*) FROM present WHERE player = $player AND items_id IS NULL AND monsters_id IS NOT NULL AND rooms_id={$array[$j]}");
$count = mysql_result($sql,0);
$sql = mysql_query("SELECT COUNT(*) FROM rooms WHERE (rooms_id={$array[$j]})");
$roompresent = mysql_result($sql,0);
if ($count == 0 and $final == -1 and $roompresent == 1){
$final = $array[$j];
}
}
} else if ($result > 200){
for ($k = 1; $k < 13; $k+=1){
$sql = mysql_query("SELECT COUNT(*) FROM present WHERE (player = $player AND items_id IS NULL AND monsters_id IS NOT NULL AND rooms_id={$array[$k]})");
$count = mysql_result($sql,0);
$sql = mysql_query("SELECT COUNT(*) FROM rooms WHERE (rooms_id={$array[$k]})");
$roompresent = mysql_result($sql,0);
if ($count == 0 and $final == -1 and $roompresent == 1){ $final = $array[$k]; }
}
if ($final == -1){
$sql = mysql_query("SELECT COUNT(*) FROM rooms");
$count = mysql_result($sql,0);
$sql = mysql_query("SELECT rooms_id FROM rooms");
for ($l = 0; $l < $count; $l+=1){
$rooms = mysql_query($sql,$l);
$sql = mysql_query("SELECT COUNT(*) FROM present WHERE player = $player AND items_id IS NULL AND monsters_id IS NOT NULL AND rooms_id=$rooms");
if (mysql_result($sql,0) == 0 and $final == -1){ $final = $rooms; }
}
}
}
if ($final != -1){ $sql = mysql_query("UPDATE present SET rooms_id=$final WHERE player = $player AND items_id IS NULL AND monsters_id=$result;"); }
else { $sql = mysql_query("UPDATE present SET rooms_id=-1 WHERE player = $player AND items_id IS NULL AND monsters_id=$result");}
}
}
$sql = mysql_query("UPDATE present SET state = 1 WHERE player = $player AND items_id IS NULL AND monsters_id IS NULL AND quantity IS NULL AND rooms_id IS NOT NULL");
} else {
$sql = mysql_query("UPDATE present SET state = 0 WHERE player = $player AND items_id IS NULL AND monsters_id IS NULL AND quantity IS NULL AND rooms_id IS NOT NULL");
}
}
当 $count 为 2 时,for 循环只运行一次。我已经通过每次 for 循环运行时将 1 添加到 mysql 表中的值来验证这一点。该值仅增加一次。
顶部的 if 语句是每隔一个滴答运行一次该函数。