我正在制作评论系统。我知道如何在数据库中插入数据以及如何选择它。我在表中显示这些数据,它工作正常。但我想在新div中显示每条记录。如何创建div并在新div中设置每个注释。
我的代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
function mydiv() {
var div = document.createElement("div");
div.style.width = "500px";
div.style.height = "100px";
div.style.background = "black";
div.style.color = "white";
div.innerHTML = "";
document.body.appendChild(div);
}
<?php
$con = mysqli_connect("localhost", "root", "", "commentdb");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM Person");
while ($row = mysqli_fetch_array($result)) {
// This is the div in which i want t diplay data.
// Should i use innerHTML or something. I totally got confused here.
echo "mydiv();";
}
mysqli_close($con);
?>
</script>
</body>
</html>
答案 0 :(得分:0)
试试这个:
<?php
$con = mysqli_connect("example.com", "peter", "abc123", "my_db"); // Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM Persons");
while ($row = mysqli_fetch_array($result)) {
echo "<div style='border:solid 1px #ccc;padding:10px;float:left;margin-top:10px;'>";
echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th> </tr>";
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
echo "</table>";
echo "</div>";
}
mysqli_close($con);
?>