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_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>";
我认为在查询数据库时读取结果集的方法存在一些问题。任何想法我在做什么错误?
你把错误放在一边(虽然这也应该解决它),你最好只做一个查询。我想改写这个为:
$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;
}
}
希望帮助
一切都看起来不错... 我看到的唯一的事情是你有没有初始化$ throughput_temp变量? 您可能希望在$ result之前和之后放置它。 这样你的变量不会从上次运行中重用。 尝试通过为while添加回显来调试循环,并计算它在$ i为1时的运行次数。
throughput_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_temp
未在for循环的顶部重新初始化。除此之外,你的代码会更清晰,没有不必要的数组嵌套。
我不知道,但我想我在你的代码中看到一个逻辑错误。
$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>";
这解决了问题一点点,但现在我需要访问值只有当我知道钥匙想打印我必须键入值 回声“值:”。 $ 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