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

时间:2009-10-13 03:34:38

标签: 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;
}

以下是有关的数据库输入:

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

我认为在查询数据库时获取结果集的方法存在一些问题。知道我做错了什么吗?

6 个答案:

答案 0 :(得分:0)

一切看起来都很好...... 我唯一看到的是你先前初始化了$ throughput_temp变量吗? 你可能想把它放在while之前和$ result之后。 这样,您的变量将不会从上次运行中重用。 尝试通过添加while的echo来调试循环,计算$ i为1时运行的次数。

答案 1 :(得分:0)

throughput_temp在哪里被初始化?它应该在for循环的开头初始化为0。

答案 2 :(得分: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 =&gt; 0,2 =&gt; 34,6 =&gt; 67,15 =&gt; 56 ...)

要遍历没有顺序(或甚至是数字)键的数组,可以使用几种方法,但最简单的方法是foreachforeach循环有两种形式:

$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和值。

答案 3 :(得分:0)

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

答案 4 :(得分: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;
  }
}

希望有所帮助

答案 5 :(得分: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>";