我在使用php + mysql從數據庫中提取信息時遇到問題,並認爲如果有人在這裏可能會提出一條出路,那將會很好。Php:通過mysql從數據庫中獲取結果集
問題的代碼:
$selectedProtocols=array(1,2);
for($i = 0; $i<2; $i++)
{
$result = mysql_query("SELECT throughput FROM session where mainProtocol='$selectedProtocols[$i]'");
while($row = mysql_fetch_array($result))
{
$throughput_temp += $row['throughput'];
}
$selectedProtocols[$selectedProtocols[$i]]=$throughput_temp;
}
以下是有關數據庫enteries:
mainProtocol name throughput
1 Skype 34
2 HTTP 43
1 FTP 54
現在,以下LOC給出正確的輸出即(34 + 54 =)88
echo "1 has throughput=".$selectedProtocols[$selectedProtocols[0]]."<br>";
但是,下面的LOC給出輸出爲零而不是43
echo "2 has throughput=".$selectedProtocols[$selectedProtocols[1]]."<br>";
我認爲在查詢數據庫時讀取結果集的方法存在一些問題。任何想法我在做什麼錯誤?
這解決了問題一點點,但現在我需要訪問值只有當我知道鑰匙想打印我必須鍵入值 回聲「值:」。 $ SP [1]; 回聲「價值:」。 $ SP [2]; 但問題是,我實際上沒有在數組中只有2個項目:SP,而我有30至40個ID,因此我不想寫30到40行來打印值。我可以用循環的幫助嗎?這樣我得到的輸出爲: 1的值是0 2的值是34 6的值是67 15的值是56 。 。 。當數組sp是(1 => 0,2 => 34,6 => 67,15 => 56 ...)時爲 。 – baltoro 2009-10-13 04:58:55