【发布时间】:2017-04-15 19:31:25
【问题描述】:
我正在尝试将信息从我的 SQL 数据库中提取到一个 html 表中。但是,当我尝试此操作时,我得到“0 个结果”,我能够连接到我的数据库,并且 SQL 在 MySQL Workbench 中运行完全正常。看来 $result 不大于 0,我不确定为什么会这样。当我没有在我的 SQL 查询中包含 Joins 时,它以前可以工作,但是就像我说它在 MySQL 工作台中工作正常。
<html>
<head><title>Employee</title>
</head>
<pre>
<body>
<center><strong><a href="manager.html">Main Page</a></strong></center>
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$conn = new mysqli($servername,$username,$password);
if($conn->connect_error){
die("connection failed: " . $conn->connect_error);
}
$sql = "SELECT first_name, last_name, email, address.address,
address.district, address.postal_code, address.phone, country.country
FROM staff
JOIN address ON staff.address_id = address.address_id
JOIN city ON address.city_id = city.city_id
JOIN country ON city.country_id = country.country_id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo"<table>";
echo("<table border = \"1\">");
print("<tr>");
print("<th>First Name</th>");
print("<th>Last Name</th>");
print("<th>Email</th>");
print("<th>Address</th>");
print("<th>District</th>");
print("<th>Postal Code</th>");
print("<th>Phone</th>");
print("<th>Country</th>");
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["staff.last_name"]. "</td><td>" . $row["staff.first_name"].
"</td><td>" . $row["staff.email"]. "</td><td>" . $row["address.address"] . "</td><td>" .
$row["address.district"] . "</td><td>" . $row["address.postal_code"] . "</td><td>" .
$row["address.phone"] . "</td><td>" . $row["country.country"] . "</td></tr>";
}
} else {
echo "0 results";
}
echo"</table>";
$conn->close();
?>
</body>
</pre>
</html>
【问题讨论】:
-
你在哪里定义和选择使用哪个数据库?
-
你不需要每行一个 echo\print