2009-10-13 64 views
0

我在使用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>"; 

我認爲在查詢數據庫時讀取結果集的方法存在一些問題。任何想法我在做什麼錯誤?

回答

0

你把錯誤放在一邊(雖然這也應該解決它),你最好只做一個查詢。我想改寫這個爲:

$selectedProtocols = array(); 
$result = mysql_query("SELECT `throughput`, `mainProtocol` FROM `session` WHERE `mainProtocol` IN (1,2)"); 

while($row = mysql_fetch_object($result)) { 
    if (!isset($selectedProtocols[$row-> mainProtocol])) { 
    $selectedProtocols[$row->mainProtocol] = $row->throughput; 
    } else { 
  $selectedProtocols[$row->mainProtocol] += $row->throughput; 
    } 
} 

希望幫助

0

一切都看起來不錯... 我看到的唯一的事情是你有沒有初始化$ throughput_temp變量? 您可能希望在$ result之前和之後放置它。 這樣你的變量不會從上次運行中重用。 嘗試通過爲while添加回顯來調試循環,並計算它在$ i爲1時的運行次數。

0

throughput_temp在哪裏被初始化?它應該在for循環開始時初始化爲0。

0

我是非常感謝您在那裏使用$selectedProtocols陣列感到困惑。

考慮這條線:

// sp = selectedProtocols ... too much typing otherwise! 

$sp[$sp[1]] 

// given that $sp == [1, 2] 
// resolve the inner 

$sp[1] == 2 // therefore: 
$sp[$sp[1]] == $sp[2] 

// but there is no $sp[2], hence the error 

我會把它改成這樣:

$sp = array(1 => 0, 2 => 0); 

foreach (array_keys($sp) as $id) { 
    $result = mysql_query("SELECT throughput FROM session where mainProtocol = '$id'"); 
    while($row = mysql_fetch_array($result)) { 
     $sp[$id] += $row['throughput']; 
    } 
} 

迴應評論:

當陣列SP是( 1 => 0,2 => 34,6 => 67,15 => 56 ...)

要遍歷不具有連續的(甚至是數字)數組中的鍵,你可以使用一些方法,但最簡單的一種是foreach。有兩種形式的foreach循環:

$array = array(1=>0, 2=>34, 6=>67, 15=>56); 

// the first form: 
foreach ($array as $value) { 
    echo $value; // "0", "34", "67", "56" 
} 

// the second form: 
foreach ($array as $key => $value) { 
    echo $key . "," . $value; // 1,0 2,34 6,67 15,56 
} 

所以你看,你可以使用第二種方法那裏得到所有的ID和值從數組。

+0

這解決了問題一點點,但現在我需要訪問值只有當我知道鑰匙想打印我必須鍵入值 回聲「值:」。 $ 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

0

$throughput_temp未在for循環的頂部重新初始化。除此之外,你的代碼會更清晰,沒有不必要的數組嵌套。

0

我不知道,但我想我在你的代碼中看到一個邏輯錯誤。

$selectedProtocols=array(1,2); 
for($i = 0; $i<2; $i++) 
{ 
$result = mysql_query("SELECT throughput FROM session where mainProtocol='$selectedProtocols[$i]'"); 
    $throughput_temp=0; 
    while($row = mysql_fetch_array($result)) 
    { 
     $throughput_temp += $row['throughput']; 
    } 
    $selectedProtocols[$selectedProtocols[$i]]=$throughput_temp; 
/* $selectedProtocols[$selectedProtocols[$i]] is equivalent to $selectedProtocol[1 or 2]. 
Therefore you are escaping the index 0 of your array and automatically starts at 1, in your 
case. So the next iteration gives you index 2 which is already out of bounds for your 
array.*/ 
    } 

試試這個代碼:

$selectedProtocols=array(1,2); 
for($i = 0; $i<2; $i++) 
{ 
    $throughput_temp = 0; 
// echo $selectedProtocols[$i]; 
    $result = mysql_query("SELECT throughput FROM session where mainProtocol='$selectedProtocols[$i]'"); 
    while($row = mysql_fetch_array($result)) 
    { 
     //echo $row['throughput']; 
     $throughput_temp += $row['throughput']; 
    } 
    $selectedProtocols[$i]=$throughput_temp; 
} 
echo "1 has throughput=".$selectedProtocols[0]."<br>"; 
echo "2 has throughput=".$selectedProtocols[1]."<br>";