Php:通过mysql从数据库获取结果集

Php:通过mysql从数据库获取结果集,php,mysql,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_

我在使用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;
}
现在,下面的LOC给出了正确的输出,即(34+54=)88

echo“1具有吞吐量=“..$selectedProtocols[$selectedProtocols[0]]”;
但是,在LOC之后,输出为零,而不是43

echo "2 has throughput=".$selectedProtocols[$selectedProtocols[1]]."<br>";
echo“2具有吞吐量=“..$selectedProtocols[$selectedProtocols[1]]”;
我认为在查询数据库时获取结果集的方法存在一些问题。知道我做错了什么吗?

一切看起来都很好。。。 我看到的唯一一件事是您是否已经在前面初始化了$throughput\u temp变量? 您可能希望将它放在$result之前和之后。 这样,您的变量就不会在上次运行时被重用。
尝试通过在一段时间内添加回显来调试循环,计算$i为1时循环运行的次数。

初始化的
吞吐量\u temp
在哪里?它应该在for循环的开头初始化为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和值。

$throughput\u temp
不会在for循环的顶部重新初始化。除此之外,如果没有不必要的数组嵌套,您的代码会更清晰。

除了bug之外(尽管这也会解决它),您最好只执行一个查询。我将改写为:

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

希望这能有所帮助,我不知道,但我想我在你的代码中看到了一个逻辑错误

$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=数组(1,2);

对于($i=0;$i)这稍微解决了一点问题,但现在我只需要在知道键喜欢打印值时访问这些值,我必须键入echo“Value:”.$sp[1];echo“Value:”.$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...)
$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;
  }
}
$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>";