2013-03-12 21 views
-4

有人可以解釋什麼時候執行此腳本到底發生了什麼。它的作用像一個魅力,但我不滿意,不知道它究竟做了什麼。有人可以解釋這個PHP的MySQL腳本?

基本上我有一個SQL表[圖片]具有以下字段

  • 類別..
  • URL

我想要做的是獲取的所有值[ URL]並將其放置在一個數組中

實現此操作我習慣了以下腳本我在這裏找到了stac koverflow

$images = mysql_query("Select URL from images where Category = 'Management' "); 
$imagerow = Array(); 

while ($row = mysql_fetch_assoc($images)) 
{ 
$imagerow[] = $row['URL']; 
} 

echo $imagerow[0] ; 

該腳本就像一個魅力,但對PHP和MySQL是新的我很難理解究竟發生了什麼。如果有人用簡單的語言解釋發生了什麼,我會喜歡它。

+1

查詢結果存儲在迭代完成的'$ images'中。 'URL'列從單個記錄中提取並存儲到一個新的佔位符'$ imagerow'中,然後根據需要使用它。 – 2013-03-12 06:10:45

+0

我無法理解爲什麼'while'循環被使用。 @ ranty的答案雖然解決了它。我很抱歉提出一個不必要的問題,只是我不相信複製粘貼代碼。 – TDsouza 2013-03-12 06:17:57

+0

@TDsouza:沒關係。只要確保你明確解釋下一次你不明白的地方。 – Mat 2013-03-12 06:36:06

回答

1

這個問題並不真的屬於這裏,但我會回答這個問題,以便在不打擾版主的情況下結束這個問題。

// mysql query is executed 
$images = mysql_query("Select URL from images where Category = 'Management' "); 

// empty array initialized 
$imagerow = Array(); 

// while there are results of the executed mysql query 
while ($row = mysql_fetch_assoc($images)) 
{ 
    // create a new element in $imagerow array and put url of the image there 
    $imagerow[] = $row['URL']; 
} 

// output first element of the array we have just filled 
echo $imagerow[0] ; 
+0

謝謝你,讓事情變得清晰。這是'時間',讓我困惑。 – TDsouza 2013-03-12 06:19:46