经过广泛的研究,我得出结论我的案例是独一无二的,我需要问。我的PHP知识非常有限,我试图仅仅根据Google的结果来完成这项工作。
目标:创建一个显示包含5列HTML表的网页(端口,L1状态,L2状态,帧错误,活动呼叫)每个列的日期存储在一个数据库表中,这就是技巧,这些数据的大部分来自同一领域...这意味着我需要创建5个不同的查询。我试图创建一个单一的查询(我相信如果可以的话,我会这样做),但我无法做到。
到目前为止的结果:仅包含第5个查询结果的表格以及表格的其余部分仅重复填充第1个查询。
这是我的代码:
<!-- Simple HTML to Create the table layout -->
<table border=1 style="background-color:#F0F8FF;" >
<caption><EM>HEADER</EM></caption>
<tr>
<th>Port</th>
<th>L1 Status</th>
<th>L2 Status</th>
<th>Framing Errors</th>
<th>Active calls</th>
</tr>
<!-- END Simple HTML to Create the table layout -->
<?php
$server = "localhost";
$dbname = "database";
$user = "user";
$password = "password";
$con = mysql_connect($server,$user,$password) or die (mysql_error());
mysql_select_db($dbname) or die (mysql_error());
$query1="select right(name, 10) as 'Port' from items where hostid = (select hostid from hosts where name = 'MIAGATE01') and key_ like '%activeChannels[%' and key_ not like '%SNMPINDEX%' order by name";
$query2="select lastvalue as 'Layer1' from items where hostid = (select hostid from hosts where name = 'MIAGATE01') and key_ like '%statusLayer1[%' and key_ not like '%SNMPINDEX%' order by name";
$query3="select lastvalue as 'Layer2' from items where hostid = (select hostid from hosts where name = 'MIAGATE01') and key_ like '%statusLayer1[%' and key_ not like '%SNMPINDEX%' order by name";
$query4="select lastvalue as 'Framing_Errors'from items where hostid = (select hostid from hosts where name = 'MIAGATE01') and key_ like '%frameErrors[%' and key_ not like '%SNMPINDEX%' order by name";
$query5="select lastvalue as 'Active_Calls' from items where hostid = (select hostid from hosts where name = 'MIAGATE01') and key_ like '%activeChannels[%' and key_ not like '%SNMPINDEX%' order by name";
$result1=mysql_query($query1) or die(mysql_error());
$result2=mysql_query($query2) or die(mysql_error());
$result3=mysql_query($query3) or die(mysql_error());
$result4=mysql_query($query4) or die(mysql_error());
$result5=mysql_query($query5) or die(mysql_error());
while($row1 = mysql_fetch_array($result1)){
while($row2 = mysql_fetch_array($result2)){
while($row3 = mysql_fetch_array($result3)){
while($row4 = mysql_fetch_array($result4)){
while($row5 = mysql_fetch_array($result5)){
echo "<tr>";
echo "<td>" . $row1['Port'] . "</td>";
echo "<td>" . $row2['Layer1'] . "</td>";
echo "<td>" . $row3['Layer2'] . "</td>";
echo "<td>" . $row4['Framing_Errors'] . "</td>";
echo "<td>" . $row5['Active_Calls'] . "</td>";
echo "</tr>";
}
}
}
}
}
mysql_close($con);
?>
答案 0 :(得分:1)
首先尝试此查询,看看它是否立即获得了所有必需的结果。
SELECT h.hostid,
RIGHT(port.name,10) AS 'Port',
l1.lastvalue AS 'Layer1',
l2.lastvalue AS 'Layer2',
fe.lastvalue AS 'Framing_Errors',
ac.lastvalue AS 'Active_Calls'
FROM hosts h
INNER JOIN items port
ON port.hostid = h.hostid
AND port.key_ LIKE '%activeChannels[%'
AND port.key_ not LIKE '%SNMPINDEX%'
INNER JOIN items l1
ON l1.hostid = h.hostid
AND l1.key_ LIKE '%statusLayer1[%'
AND l1.key_ not LIKE '%SNMPINDEX%'
INNER JOIN items l2
ON l2.hostid = h.hostid
AND l2.key_ LIKE '%statusLayer2[%'
AND l2.key_ not LIKE '%SNMPINDEX%'
INNER JOIN items fe
ON fe.hostid = h.hostid
AND fe.key_ LIKE '%frameErrors[%'
AND fe.key_ not LIKE '%SNMPINDEX%'
INNER JOIN items ac
ON ac.hostid = h.hostid
AND ac.key_ LIKE '%activeChannels[%'
AND ac.key_ not LIKE '%SNMPINDEX%'
WHERE h.name = 'MIAGATE01'
ORDER BY h.name;
如果这样做,您只需要一个while
循环来填充表格。
while ( $row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['Port'] . "</td>";
echo "<td>" . $row['Layer1'] . "</td>";
echo "<td>" . $row['Layer2'] . "</td>";
echo "<td>" . $row['Framing_Errors'] . "</td>";
echo "<td>" . $row['Active_Calls'] . "</td>";
echo "</tr>";
}
答案 1 :(得分:0)
首先:
您的预期HTML表格的每个ROW都包含所有五个数据项吗?问另一种方式,每个端口有一行吗?你的问题并不明显。从您的逻辑中可以确定不同参数的出现次数不同。
无论如何......你需要将一堆包含hostid的参数连接在一起,将你的显示放在一起。我想是这样的。 (您的数据非常复杂)。
select hostid.hostid, port.Port, layer1.Layer1, layer2.Layer2
from
(select hostid, name
from hosts
where name = 'MIAGATE01'
) hostid
left join
(select hostid, right(name,10) as 'Port'
from items
where key_ like '%activeChannels[%' and key_ not like '%SNMPINDEX%'
) port
on hostid.hostid = port.hostid
left join
(select hostid, lastvalue as 'Layer1'
from items
where key_ like '%statusLayer1[%' and key_ not like '%SNMPINDEX%'
) layer1
on hostid.hostid = layer1.hostid
left join
(select hostid, lastvalue as 'Layer2'
from items
where key_ like '%statusLayer2[%' and key_ not like '%SNMPINDEX%'
) layer2
on hostid.hostid = layer2.hostid
order by hostid.name, port.Port
看看怎么样?您有很多小选项可以生成值列表值与您的key_选择条件匹配的hostid值项目列表。然后,您将所有这些加入到他们的hostid值中。第一个小选择只是为你想要的特定主机提供了正确的hostid。
我没有完成你的所有参数,但是你不应该很难完成这个查询。至于它的调试......如果没有看到你的items
表,那很难知道。这可能是一个很大的毛球。但你已经知道了。
网络设备的日志是不是只有大桶充满乐趣?