【问题标题】:Populate HTML Table with MySQL data (Multiple Queries)使用 MySQL 数据填充 HTML 表(多个查询)
【发布时间】:2012-06-06 04:43:48
【问题描述】:

经过广泛的研究,我得出的结论是我的案例是独一无二的,我需要问一下。我的 PHP 知识非常有限,我正试图仅根据 Google 搜索结果来完成这项工作。

目标:创建一个网页,显示一个包含 5 列(端口、L1 状态、L2 状态、帧错误、活动呼叫)的 HTML 表。这些列中的每一列的日期都存储在一个数据库表中,这就是诀窍,这些数据的大部分来自同一个字段...这意味着我需要创建 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);
?>

【问题讨论】:

  • 更好的问题是围绕进行单个查询,然后从那里开始
  • 您必须先收集并存储每个mysql_query 的结果,然后再执行另一个。
  • 天哪,你有 5 个嵌套的 while :o
  • 修复您的数据库设计,将这些值放在自己的列中。

标签: php mysql database html-table


【解决方案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>";
}

【讨论】:

  • 看起来不错。但是如果缺少某些参数,INNER JOIN 会隐藏整个结果行。
  • 没错。但这个问题听起来好像所有数据都可用于每个 hostid。如果没有,你当然要把所有的INNER JOINs 改成LEFT JOINs。
  • 感谢您的回复。我完全理解必须使用多个 while 循环效率不高,最好的选择是编译一个查询,它会给我想要的结果。上面的查询确实给了我多行数据,但它只为一个“端口”输出数据,我正在努力调整您提供的查询。我会看看我是否能正常工作,我会在这里发布。
  • 我创建了一个新帖子寻求有关 SQL 查询的帮助(显示表布局和所需结果,检查是否有帮助):stackoverflow.com/questions/10853565/…
【解决方案2】:

首先:

您预期的 HTML 表格的每一行是否都包含所有五个数据项?换一种方式问,每个端口是否有一行?从你的问题中看不出来。从您的逻辑来看,不同参数的出现次数当然可能不同。

无论如何...您需要将一堆包含 hostid,parameter 的虚拟表连接在一起,以组合您的显示。像这样,我想。 (您的数据相当复杂)。

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

看看情况如何?您有很多小选择会生成 hostid 列表,其中值项与您的 key_ 选择条件匹配。然后,您将它们的 hostid 值结合在一起。第一个小选择只是为您想要的特定主机提供正确的 hostid。

我没有做你所有的参数,但你应该不难完成这个查询。至于调试它......如果没有看到您的items 表的所有荣耀,就很难知道。这可能是一个大毛球。但你已经知道了。

网络设备日志不就是一个充满乐趣的大桶吗?

【讨论】:

  • 好的,表格布局如下:
猜你喜欢
  • 2013-07-30
  • 2018-04-13
  • 2018-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多