Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I`m trying to read data from a MySQL table. I want it to show on a page in a nice table, but what happens is that every row from the MySQL table, creates a new HTML table.

I have been playing with the code, and found a "possible" solution on another website, but I don`t get it to work. It says that opening the table should happen before the while part, but then I get the first row in a HTML table, and then all ohter rows/data under the HTML table.

This is the code I use now:

data.php

<?php Header ("Content-type: text/css; charset=utf-8");?>
<link rel="stylesheet" href="style.css">
<?php 
$conn = new mysqli('host', 'user', 'password', 'database');
if ($conn->connect_error) {
	die("Connection error: " . $conn->connect_error);
}

$result = $conn->query("SELECT * FROM table");
if ($result->num_rows > 0) {
	while ($row = $result->fetch_assoc()) {
{
  echo '<table class="table">'; 
  echo "<tr><th>Name</th><th>Description:</th><th>Status</th></tr>";
  echo "<tr><td>"; 
  echo $row['Name'];
  echo "</td><td>";   
  echo $row['Description'];
  echo "</td><td>";    
  echo $row['Status'];
  echo "</td></tr>";
 
}
  echo "</table>";
	}
}
?>


index.php

HTML
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title></title>
	
</head>
<body>
	<div id="show"></div>

	<script type="text/javascript" src="jquery.js"></script>
	<script type="text/javascript">
		$(document).ready(function() {
			setInterval(function () {
				$('#show').load('data.php')
			}, 3000);
		});
	</script>
</body>
</html>


What I have tried:

I have searched on Google and found possible solutions that don`t seem to work with this code.
Posted
Updated 7-Mar-16 3:31am

You have to be careful to output the opening table tag only once before the while loop and the corresponding closing table tag only once at the end of the loop, e.g.
if ($result->num_rows > 0) {
  // output the opening table tag only once before the loop
  echo '<table class="table">';
  echo "<tr><th>Name</th><th>Description:</th><th>Status</th></tr>";
  while ($row = $result->fetch_assoc()) {
    echo "<tr><td>"; 
    echo $row['Name'];
    echo "</td><td>";   
    echo $row['Description'];
    echo "</td><td>";    
    echo $row['Status'];
    echo "</td></tr>";
  }
  // output the closing table tag only once at the end of loop
  echo "</table>";
}
 
Share this answer
 
v3
Thank you Peter Leow!

It still showed a table for each row, but now atleast it looked decent. Just had to move the
echo "<tr><th>Name</th><th>Description:</th><th>Status</th></tr>";

above the while, so:

if ($result->num_rows > 0) {
  // output the opening table tag only once before the loop
  echo '<table class="table">';
  echo "<tr><th>Name</th><th>Description:</th><th>Status</th></tr>";
  while ($row = $result->fetch_assoc()) {
    echo "<tr><td>"; 
    echo $row['Name'];
    echo "</td><td>";   
    echo $row['Description'];
    echo "</td><td>";    
    echo $row['Status'];
    echo "</td></tr>";
  }
  // output the closing table tag only once at the end of loop
  echo "</table>";
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900