gpt4 book ai didi

php - 如何让我的 PHP 代码显示在 html 表格中?

转载 作者:太空宇宙 更新时间:2023-11-04 02:45:59 25 4
gpt4 key购买 nike

我有一个 php 代码可以显示来自 SQL 数据库的名称和编号。我有一个包含名称和编号列的 html 表。我希望我的 SQL 数据库中的名称和编号显示在 HTML 表格中。

PHP代码

<?php
$servername = "localhost";
$username = "what7aba_wc";
$password = "This is my Password";
$dbname = "what7aba_wc";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT name, number FROM WC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Name" . $row["name"]. "-Number" . $row["number"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>

HTML 表格

  <table id="table" class="table table-hover table-mc-light-blue">
<thead>
<tr>
<th>Name</th>
<th>Link</th>
</tr>
</thead>
<tbody>
<tr>
<td data-title="Name">Its a Name</td>
<td data-title="Number">

</tr>
<tr>
<td data-title="Name">Its a Name</td>
<td data-title="Number">

</tr>
</tbody>
</table>

如果这不可能发生。谁能告诉我如何为上面给出的 PHP 文件制作 CSS。

更新:

我试过了。

<?php
$servername = "localhost";
$username = "what7aba_wc";
$password = "My Password";
$dbname = "what7aba_wc";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT name, number FROM WC";
$result = $conn->query($sql);

?>
<h1>WhatsCast List</h1>
<h2>Table of my other Material Design works (list was updated 08.2015)</h2>

<div class="table-responsive-vertical shadow-z-1">
<table id="table" class="table table-hover table-mc-light-blue">
<thead>
<tr>
<th>Name</th>
<th>Link</th>
</tr>
</thead>
<tbody>
<tr>
<td data-title="Name"><p><?php echo $row["name"] ?><p></td>
<td data-title="Number"><p><?php echo $row["number"] ?></p></td>
</tr>

</tbody>
</table>
</div>

</div>

最佳答案

HTML

<table id="table" class="table table-hover table-mc-light-blue">
<thead>
<tr>
<th>Name</th>
<th>Link</th>
</tr>
</thead>
<tbody>
<?php
while($row = $result->fetch_assoc()) { ?>
<tr>
<td data-title="Name"><?php echo $row["name"]?></td>
<td data-title="Number"><?php echo $row["number"] ?></td>
</tr>
<? } ?>
</tbody>
</table>

关于php - 如何让我的 PHP 代码显示在 html 表格中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33843230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com