2016-04-03 78 views
0

我想在index.php中显示2个表。显示来自1个数据库的相同数据。但它没有出现。见下面在index.php中显示2个表mysql

click here to see image

这里形象是我的代码(的index.php)

<?php 
 

 
\t include("dbcon.php"); \t 
 
\t 
 
\t $link=$cn; 
 
    $i=0; 
 
\t $result=mysqli_query($link,"SELECT * FROM `node` ORDER BY `tarikh` DESC LIMIT 1"); 
 
\t 
 
?> 
 

 
<html> 
 
    <head> 
 
     <title>Sensor Data1</title> 
 
     <meta http-equiv="refresh" content="30"> 
 
    </head> 
 
<body> 
 
    <Center><h1>Sensor Readings</h1> 
 
    <h1>Node 1</h2> 
 

 

 
    <table border="2" cellspacing="2" cellpadding="2"> 
 
\t \t <tr> 
 
\t \t \t <td>&nbsp;No&nbsp;</td> 
 
\t \t \t <td>&nbsp;Waktu/Tarikh&nbsp;</td> 
 
\t \t \t <td>&nbsp;Temperature &nbsp;</td> 
 
\t \t \t <td>&nbsp;Humidity &nbsp;</td> 
 
\t \t \t <td>&nbsp;Length(CM)&nbsp;</td> 
 
\t \t \t <td>&nbsp;Node&nbsp;</td> \t 
 
\t \t </tr> 
 

 
\t \t 
 
     <?php 
 
\t \t  while($row = mysqli_fetch_array($result)) { 
 
\t \t   printf("<tr><td> &nbsp;%d </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td></tr>", 
 
\t \t   ++$i, $row["tarikh"], $row["temperature"], $row["humidity"], $row["height"], $row["node"]); 
 
\t \t  } \t \t 
 
\t \t 
 
     ?> 
 

 
    </table></center> 
 
    
 
    <center><table border="2" cellspacing="2" cellpadding="2"> 
 
\t \t <tr> 
 
\t \t \t <td>&nbsp;No&nbsp;</td> 
 
\t \t \t <td>&nbsp;Waktu/Tarikh&nbsp;</td> 
 
\t \t \t <td>&nbsp;Temperature &nbsp;</td> 
 
\t \t \t <td>&nbsp;Humidity &nbsp;</td> 
 
\t \t \t <td>&nbsp;Length(CM)&nbsp;</td> 
 
\t \t \t <td>&nbsp;Node&nbsp;</td> \t 
 
\t \t </tr> 
 
\t \t 
 
\t \t <?php 
 
\t \t  while($row = mysqli_fetch_array($result)) { 
 
\t \t   printf("<tr><td> &nbsp;%d </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td></tr>", 
 
\t \t   ++$i, $row["tarikh"], $row["temperature1"], $row["humidity1"], $row["height"], $row["node"]); 
 
\t \t  } \t \t 
 
\t \t 
 
     ?></center> 
 
</body> 
 
</html>

可以纠正我的代码,使其出现?

回答

0

我相信你的第二张表没有打印的原因是$结果移动到第一个while循环的结尾。

如果我是正确的,快速简便的解决方案是在第二个循环之前重新运行sql查询,如下所示。

<?php 
 

 
\t include("dbcon.php"); \t 
 
\t 
 
\t $link=$cn; 
 
    $i=0; 
 
\t $result=mysqli_query($link,"SELECT * FROM `node` ORDER BY `tarikh` DESC LIMIT 1"); 
 
\t 
 
?> 
 

 
<html> 
 
    <head> 
 
     <title>Sensor Data1</title> 
 
     <meta http-equiv="refresh" content="30"> 
 
    </head> 
 
<body> 
 
    <Center><h1>Sensor Readings</h1> 
 
    <h1>Node 1</h2> 
 

 

 
    <table border="2" cellspacing="2" cellpadding="2"> 
 
\t \t <tr> 
 
\t \t \t <td>&nbsp;No&nbsp;</td> 
 
\t \t \t <td>&nbsp;Waktu/Tarikh&nbsp;</td> 
 
\t \t \t <td>&nbsp;Temperature &nbsp;</td> 
 
\t \t \t <td>&nbsp;Humidity &nbsp;</td> 
 
\t \t \t <td>&nbsp;Length(CM)&nbsp;</td> 
 
\t \t \t <td>&nbsp;Node&nbsp;</td> \t 
 
\t \t </tr> 
 

 
\t \t 
 
     <?php 
 
\t \t  while($row = mysqli_fetch_array($result)) { 
 
\t \t   printf("<tr><td> &nbsp;%d </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td></tr>", 
 
\t \t   ++$i, $row["tarikh"], $row["temperature"], $row["humidity"], $row["height"], $row["node"]); 
 
\t \t  } \t \t 
 
\t \t 
 
     ?> 
 

 
    </table></center> 
 
    
 
    <center><table border="2" cellspacing="2" cellpadding="2"> 
 
\t \t <tr> 
 
\t \t \t <td>&nbsp;No&nbsp;</td> 
 
\t \t \t <td>&nbsp;Waktu/Tarikh&nbsp;</td> 
 
\t \t \t <td>&nbsp;Temperature &nbsp;</td> 
 
\t \t \t <td>&nbsp;Humidity &nbsp;</td> 
 
\t \t \t <td>&nbsp;Length(CM)&nbsp;</td> 
 
\t \t \t <td>&nbsp;Node&nbsp;</td> \t 
 
\t \t </tr> 
 
\t \t 
 
\t \t <?php 
 

 

 
\t \t  $result=mysqli_query($link,"SELECT * FROM `node` ORDER BY `tarikh` DESC LIMIT 1"); 
 
\t 
 
\t \t  while($row = mysqli_fetch_array($result)) { 
 
\t \t   printf("<tr><td> &nbsp;%d </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td></tr>", 
 
\t \t   ++$i, $row["tarikh"], $row["temperature1"], $row["humidity1"], $row["height"], $row["node"]); 
 
\t \t  } \t \t 
 
\t \t 
 
     ?></center> 
 
</body> 
 
</html>

虽然我从来没有做过用这个(我刚刚发现它具有快速搜索重置$结果)。你可以使用mysqli_data_seek($ result,0);这会更快,我认为它不需要重新获取数据。