2012-01-06 48 views
0

創建一個簡單的數組我有下面的PHP代碼:使用我的數據庫

$seasons = array("Autumn", "Winter", "Spring", "Summer"); 

現在我也有一個表型數據庫,在那裏四季如:

+--------+ 
| Season | 
+--------+ 
| Autumn | 
| Winter | 
| Spring | 
| Summer | 
+--------+ 

所以我想從我的數據庫中獲取季節並將它們放入這個數組中,但是如何做到這一點。自己嘗試失敗

得到這個對於初學者(使用whilemysql_fetch_arraymysql_fetch_ASSOC,等試過):

$result = mysql_query("SELECT Season FROM Seasons ORDER BY Season"); 

這應該給我所有的賽季,但我在這裏停留。在網上搜索,但例子也有辦法我需要什麼太複雜......

非常感謝

+0

'mysql_query'爲您提供了一個結果集,您需要使用'mysql_fetch_ *'輪詢行,PHP手冊非常清晰。你能否顯示一些你已經嘗試過的代碼,但沒有工作? – Eric 2012-01-06 23:13:28

回答

1

試試這段代碼。它將使用查詢結果填充名爲$ seasons的數組。

$result = mysql_query("SELECT Season FROM Seasons ORDER BY Season"); 
while($row = mysql_fetch_array($result)){ 
    $season=$row['Season']; 
    $seasons[] = $season; 
} 

它似乎對我很有用。

+0

這似乎工作,謝謝 – Maurice 2012-01-07 22:21:07

4
$seasons = array(); 
$result = mysql_query("SELECT Season FROM Seasons ORDER BY Season"); 
while($row = mysql_fetch_num($result)) 
    $seasons[] = row[0]; 
+0

夥計,你重寫$結果? – 2012-01-06 23:11:50

+0

@Lior:已經修好了^^ – yankee 2012-01-06 23:13:14

+0

對。你仍然有一個錯字:P – 2012-01-06 23:13:57

0
while ($row = mysql_fetch_assoc($result)) { 
    echo $row['Season'] . "\n"; 
} 

這將遍歷一次的結果一行。當沒有行時,mysql_fetch_assoc返回false,並退出循環。

相關問題