【发布时间】:2016-07-21 20:53:27
【问题描述】:
我在html 中有一个列表,定义如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php include "connect.php"; ?>
<div id="view">
<div id="container">
<ul>
<!-- row 01 -->
<a href="#"><li class="clearfix">
<img src="images/modern-castle-kitchen.png" alt="thumb" class="thumbnail">
<h2>Full-Room Mansion with Open Kitchen</h2>
<p class="desc">Rental located in Pheonix, AZ. 2 bedrooms 1.5 baths. </p>
<span class="price">S2,650/month</span>
</li></a>
</ul>
</div>
</div>
</body>
</html>
另一个 connect.php 文件,用于配置与我的数据库的连接
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "room_seeker";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT image, detail, money FROM room";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "image: " . $row["image"]. " - detail: " . $row["detail"]. " " . "money:". $row["money"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
在我上面的 html 文件中,我只是定义了一个固定的数据。如何编写代码以从我的数据库 (room_seeker.sql) 中获取数据并插入到我上面的 html 文件中的列表中。谢谢你的帮助
更新:根据建议,我将我的 html 文件更新为
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php include "connect.php"; ?>
<div id="view">
<div id="container">
<ul>
<!-- Assume sql has five rooms, so each room will be displayed in one list -->
<a href="#"><li class="clearfix">
<img src="images/modern-castle-kitchen.png" alt="thumb" class="thumbnail">
<h2><?php echo $row['image']?></h2>
<p class="desc"><?php echo $row['detail']?></p>
<span class="price"><?php echo $row['money']?></span>
</li></a>
</ul>
</div>
</div>
</body>
</html>
但输出错误(页面中没有任何内容)
【问题讨论】:
-
在
$row数据点周围回显HTML,使其格式符合预期。 -
你能说得更清楚一点吗?我假设我有 5 个来自数据库的房间。如何将它们全部列出到 html 中的 5 个列表中?我尝试了代码,但它不起作用
-
您能否更新问题,说明该代码的使用位置以及发生了什么?