我尝试将XML文件的温度与我的数据库进行比较。 但它只是比较了7个数据中的第一个。 这意味着while循环仅适用于第一遍,但为什么?
$query = mysql_query("SELECT * FROM table"); //get temperatures from databse
foreach ($xml->forecast as $forecast) { //just one pass (one forecast in the xml)
foreach ($forecast->time as $time) { // 7 passes (7 dates in the xml)
echo $time['day'] . "<br />";
while ($row = mysql_fetch_array($query)) { //This loop just works on the first pass
if ($row['mintemp'] <= $time->temperature['day'] && $time->temperature['day'] <= $row['maxtemp']) {
echo $row['namekl'] . " | Rating (" . $row['rating'] . ")" . "<br />";
}
}
echo "<br />";
}
}
我通常期望得到以下结果:
2013-07-19
Databse 1 | Rating (5)
Databse 2 | Rating (5)
Databse 3 | Rating (3)
2013-07-20
Databse 1 | Rating (5)
Databse 2 | Rating (5)
Databse 3 | Rating (3)
2013-07-21
Databse 1 | Rating (5)
Databse 2 | Rating (5)
Databse 3 | Rating (3)
2013-07-22
Databse 1 | Rating (5)
Databse 2 | Rating (5)
Databse 3 | Rating (3)
2013-07-23
Databse 1 | Rating (5)
Databse 2 | Rating (5)
Databse 3 | Rating (3)
2013-07-24
Databse 1 | Rating (5)
Databse 2 | Rating (5)
Databse 3 | Rating (3)
2013-07-25
Databse 1 | Rating (5)
Databse 2 | Rating (5)
Databse 3 | Rating (3)
但我的结果如下:
2013-07-19
Databse 1 | Rating (5)
Databse 2 | Rating (5)
Databse 3 | Rating (3)
2013-07-20
2013-07-21
2013-07-22
2013-07-23
2013-07-24
2013-07-25
答案 0 :(得分:2)
问题不在于foreach
循环,而在于你如何使用MySQL。第一次调用时,mysql_fetch_array
会返回整个表中的所有结果,然后在循环中的每一次传递都没有任何结果...