在index.php-mysql中显示2个表


display 2 table in index.php mysql

I希望在index.php中显示2个表。显示1个数据库中的相同数据。但它没有出现。参见下方的图片

单击此处查看图像

这是我的代码(index.php)

<?php
	include("dbcon.php"); 	
	
	$link=$cn;
    $i=0;
	$result=mysqli_query($link,"SELECT * FROM `node` ORDER BY `tarikh` DESC LIMIT 1");
	
?>
<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">
		<tr>
			<td>&nbsp;No&nbsp;</td>
			<td>&nbsp;Waktu / Tarikh&nbsp;</td>
			<td>&nbsp;Temperature &nbsp;</td>
			<td>&nbsp;Humidity &nbsp;</td>
			<td>&nbsp;Length(CM)&nbsp;</td>
			<td>&nbsp;Node&nbsp;</td>	
		</tr>
		
      <?php 
		     while($row = mysqli_fetch_array($result)) {
		        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>", 
		           ++$i, $row["tarikh"], $row["temperature"], $row["humidity"], $row["height"], $row["node"]);
		     }		
		  
      ?>
   </table></center>
   
    <center><table border="2" cellspacing="2" cellpadding="2">
		<tr>
			<td>&nbsp;No&nbsp;</td>
			<td>&nbsp;Waktu / Tarikh&nbsp;</td>
			<td>&nbsp;Temperature &nbsp;</td>
			<td>&nbsp;Humidity &nbsp;</td>
			<td>&nbsp;Length(CM)&nbsp;</td>
			<td>&nbsp;Node&nbsp;</td>	
		</tr>
		
		<?php 
		     while($row = mysqli_fetch_array($result)) {
		        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>", 
		           ++$i, $row["tarikh"], $row["temperature1"], $row["humidity1"], $row["height"], $row["node"]);
		     }		
		  
      ?></center>
</body>
</html>

可以更正我的代码使其出现吗?

我相信第二个表没有打印的原因是$result在第一个while循环中被移到了末尾。

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

<?php
	include("dbcon.php"); 	
	
	$link=$cn;
    $i=0;
	$result=mysqli_query($link,"SELECT * FROM `node` ORDER BY `tarikh` DESC LIMIT 1");
	
?>
<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">
		<tr>
			<td>&nbsp;No&nbsp;</td>
			<td>&nbsp;Waktu / Tarikh&nbsp;</td>
			<td>&nbsp;Temperature &nbsp;</td>
			<td>&nbsp;Humidity &nbsp;</td>
			<td>&nbsp;Length(CM)&nbsp;</td>
			<td>&nbsp;Node&nbsp;</td>	
		</tr>
		
      <?php 
		     while($row = mysqli_fetch_array($result)) {
		        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>", 
		           ++$i, $row["tarikh"], $row["temperature"], $row["humidity"], $row["height"], $row["node"]);
		     }		
		  
      ?>
   </table></center>
   
    <center><table border="2" cellspacing="2" cellpadding="2">
		<tr>
			<td>&nbsp;No&nbsp;</td>
			<td>&nbsp;Waktu / Tarikh&nbsp;</td>
			<td>&nbsp;Temperature &nbsp;</td>
			<td>&nbsp;Humidity &nbsp;</td>
			<td>&nbsp;Length(CM)&nbsp;</td>
			<td>&nbsp;Node&nbsp;</td>	
		</tr>
		
		<?php 
		     $result=mysqli_query($link,"SELECT * FROM `node` ORDER BY `tarikh` DESC LIMIT 1");
	
		     while($row = mysqli_fetch_array($result)) {
		        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>", 
		           ++$i, $row["tarikh"], $row["temperature1"], $row["humidity1"], $row["height"], $row["node"]);
		     }		
		  
      ?></center>
</body>
</html>

尽管我从未使用过这个(我只是在快速搜索重置$result时发现的)。您可能可以使用mysqli_data_seek($result,0);我认为这会更快,因为它不需要重新获取数据。