您好我有一个页面,我可以从mysql数据库查看数据输出。它运作良好。但它不时尚。所以我决定加入一个html <table>
标签。但是它没有显示数据。
我尝试过进入
行之间的 <td> </td>
等但它会停止正在解析的代码。
<?php
$servername = "******";
$username = "root";
$password = "********";
$dbname = "test2";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT first_name, last_name, email FROM person_list";
$result = $conn->query($sql);
//display table
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo
"<br> ". $row["first_name"]. " "
. $row["last_name"] ." "
. $row["email"] ." " ;
}
}
else {
echo "0 results";
}
echo "</table>";
$conn->close();
?>
答案 0 :(得分:3)
试试这个,如果它对你有用..
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>". $row["first_name"] . "</td>";
echo "<td>". $row["last_name"] . "</td>";
echo "<td>". $row["email"] . "</td> " ;
echo "</tr>";
}
}
echo "</table>";
答案 1 :(得分:0)
你也可能对foreach方式感兴趣。
Foreach在更大的数据集中会更快,但这与Narendrasingh Sisodia的回答非常相似。
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>";
if($result->num_rows > 0){
foreach($result as $row){
echo "<tr>";
echo "<td>". $row["first_name"] . "</td>";
echo "<td>". $row["last_name"] . "</td>";
echo "<td>". $row["email"] . "</td> " ;
echo "</tr>";
}
}
echo "</table>";