我有以下php代码:
$seasons = array("Autumn", "Winter", "Spring", "Summer");
现在我还有一个带有桌子季节的数据库,在那里的季节如下:
+--------+
| Season |
+--------+
| Autumn |
| Winter |
| Spring |
| Summer |
+--------+
所以我想从我的数据库中获取季节并将它们放在这个数组中,但是如何做到这一点。自己尝试失败(尝试使用while
,mysql_fetch_array
,mysql_fetch_ASSOC
等)
为初学者做到了这一点:
$result = mysql_query("SELECT Season FROM Seasons ORDER BY Season");
这应该给我所有的季节,但我被困在这里。搜索了互联网,但有些例子太复杂了我需要的东西......
非常感谢
答案 0 :(得分:4)
$seasons = array();
$result = mysql_query("SELECT Season FROM Seasons ORDER BY Season");
while($row = mysql_fetch_num($result))
$seasons[] = row[0];
答案 1 :(得分:1)
尝试这段代码。它将使用您的查询结果填充一个名为$ seasons的数组。
$result = mysql_query("SELECT Season FROM Seasons ORDER BY Season");
while($row = mysql_fetch_array($result)){
$season=$row['Season'];
$seasons[] = $season;
}
它似乎对我有用。
答案 2 :(得分:0)
while ($row = mysql_fetch_assoc($result)) {
echo $row['Season'] . "\n";
}
这将一次循环一行结果。如果没有剩余行,mysql_fetch_assoc
会返回false
,并退出循环。