-1

我尝试将 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
4

1 回答 1

2

问题不在于foreach循环,而在于您如何使用 MySQL。mysql_fetch_array在您第一次调用它时返回整个表中的所有结果,然后在每个其他循环中都没有返回任何内容......

于 2013-07-19T10:30:22.733 回答