在index.php mysql中显示2个表

时间:2016-04-03 04:13:41

标签: php mysql

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

click here to see image

这是我的代码(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>
&#13;
&#13;
&#13;

可以更正我的代码以使其显示吗?

1 个答案:

答案 0 :(得分:0)

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

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

&#13;
&#13;
<?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>
&#13;
&#13;
&#13;

虽然我从未使用过这个(我只是通过快速搜索重置$结果找到它)。你可以使用mysqli_data_seek($ result,0);我觉得这会更快,因为它不需要重新获取数据。