[英]getting data from database to html table using php
我想從數據庫中獲取數據並將其插入到我的html表中,這是我的代碼,我不知道我的錯誤在哪里:
<div class="ibox-content">
<?php
$servername = "localhost";
$username = "sehnoqta_userbmc";
$password = "u?gQ=uS%t;a?";
$dbname = "sehnoqta_bmc";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT name, lastname, phone FROM regis";
$result = $conn->query($sql);
$conn->close();
?>
<table dir="rtl" class="table table-striped table-bordered table-hover dataTables-example" >
<thead>
<tr>
<th>Name</th>
<th>Last Name</th>
<th>Phone</th>
<th>Email</th>
<th>Acc Type</th>
</tr>
</thead>
<tbody>
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo
"<tr><td>" . $row["name"]. "</td>
<td>" . $row["lastname"]. "</td>
<td>" . $row["phone"]. "</td></tr>";
<td>0795934799</td>
<td class="center">demo@demo.com</td>
<td>Admin</td>
}
echo "</table>";
}
?>
</tbody>
</table>
</div>
我從數據庫中獲取的數據顯示在表外。 對不起,英語不好:)
while循環中只有一個語法錯誤。 您需要在echo命令中打印所有內容。 如您所見,您具有echo(...);
其次是仍在php中的HTML。 因此,您應該通過將其更改為以下內容來進行更正。
while($row = $result->fetch_assoc()) {
echo
"<tr><td>" . $row["name"]. "</td>
<td>" . $row["lastname"]. "</td>
<td>" . $row["phone"]. "</td></tr>
<td>0795934799</td>
<td class=\"center\">demo@demo.com</td>
<td>Admin</td>";
}
可以使用的好資源是phpchecker.com ,它可以檢查您的代碼是否有錯誤
這是有效的代碼...
<?php
$servername = "localhost";
$username = "sehnoqta_userbmc";
$password = "u?gQ=uS%t;a?";
$dbname = "sehnoqta_bmc";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT name, lastname, phone FROM regis";
$result = $conn->query($sql);
$conn->close();
?>
<table dir="rtl" class="table table-striped table-bordered table-hover dataTables-example" >
<thead>
<tr>
<th>Name</th>
<th>Last Name</th>
<th>Phone</th>
<th>Email</th>
<th>Acc Type</th>
</tr>
</thead>
<tbody>
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo
"<tr><td>" . $row["name"]. "</td>
<td>" . $row["lastname"]. "</td>
<td>" . $row["phone"]. "</td>";
"<td>0795934799</td>";
"<td class='center'>demo@demo.com</td>";
"<td>Admin</td></tr>";
}
echo "</table>";
}
希望這可以幫助......
echo
"<tr><td>" . $row["name"]. "</td>
<td>" . $row["lastname"]. "</td>
<td>" . $row["phone"]. "</td>
<td>0795934799</td>
<td class='center'>demo@demo.com</td>
<td>Admin</td></tr>";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.