Php: mysql aracılığıyla veritabanından sonuç almak setleri

5 Cevap php

Ben php + mysql kullanarak bir veritabanından bilgi ayıklanması sırasında bir sorunla karşılaşıyor ve burada birileri bir yol önerebilir eğer iyi olacağını düşündüm duyuyorum.

Problematic Code:

$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;
}

Ilgili veritabanı enteries şunlardır:

mainProtocol  name  throughput
1             Skype 34
2             HTTP  43
1             FTP   54

Şimdi, aşağıdaki LOC doğru çıktı ie (34 +54 =) 88 verir

echo "1 has throughput=".$selectedProtocols[$selectedProtocols[0]]."<br>";

Ancak, aşağıdaki LOC yerine 43 sıfır olarak verir çıktıya

echo "2 has throughput=".$selectedProtocols[$selectedProtocols[1]]."<br>";

Ben veritabanı sorgulama sırasında sonuç kümesi getiriliyor yöntemi ile bazı sorun olduğunu düşünüyorum. Herhangi bir fikir sorun ben ne yapıyorum?

5 Cevap

(Bu da düzeltmek gerekir rağmen) bir kenara hata sensin, you would be better off only doing one query. Ben bu yeniden olacaktır:

$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;
  }
}

Umut olur

Nerede throughput_temp başlatıldı ediliyor? Bu döngü için başında 0 başlangıç ​​olmalıdır.

$throughput_temp döngüsü için üstündeki yeniden başlatılmış değildir. Bunun yanı sıra, kod gereksiz dizi iç içe olmadan net olurdu.

Bilmiyorum ama ben senin kodunda bir mantık hatası görüyorum.

$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.*/
    }

Bu kodu deneyin:

 $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>";

I very var $selectedProtocols dizisinin kullanımınız kafamı karıştırdı.

Bu hat düşünün:

// 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

Ben bu onu değiştirmek istiyorsunuz:

$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'];
    }
}


Response to comment:

dizi sp (1 => 0,2 => 34,6 => 67,15 => 56 ...) olduğunda

Ardışık (hatta sayısal) tuşları yok bir dizi döngü için, birkaç yöntem kullanabilirsiniz, ancak kolay bir foreach olduğunu. foreach döngünün iki biçimi vardır:

$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
}

Yani diziden tüm kimlikleri ve değerleri almak için orada ikinci bir yöntem kullanabilirsiniz görüyoruz.