gpt4 book ai didi

PHP 不将数据库打印为表

转载 作者:行者123 更新时间:2023-11-29 09:24:46 24 4
gpt4 key购买 nike

我正在尝试将 mySQL 数据库打印到 html 表中。我看过很多关于如何执行此操作的教程,但不确定如何在 php 代码中引用 html 表。信息可以正常打印并连接到数据库,但由于某种原因,它没有以表格格式输出。

<?php
$conn = mysqli_connect('localhost', 'Admin', 'admin1', 'info');
if (!$conn) {
echo "Connection failed:" . mysqli_connect_error();
}
//Writing query for database.
$sql = "SELECT `First Name`,`Last Name`,Emails,`Date Created` FROM clientinfo ORDER BY `Date Created`";

//Querying and getting results
$result = mysqli_query($conn, $sql);

if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["First Name"] . "</td></tr>" . $row["Last Name"] . "</td></tr>"
. $row["Emails"] . "</td></tr>" . $row["Date Created"] . "</td></tr>";
}
echo "</table>";
} else {
echo "0 result";
}

//Fetch resulting rows as an array
$informed = mysqli_fetch_all($result, MYSQLI_ASSOC);

// Freeing result from the memory.
mysqli_free_result($result);

mysqli_close($conn);
?>

<!DOCTYPE html>
<html lang="en-US">
<head>
<div class="Contained">
<div class="row">
<?php foreach ($informed as $inform) { ?>
<div class="col s6 medium-3">
<div class="card z-depth-0">
<div class="card-content center">
<h6><?php echo htmlspecialchars($inform['First Name']); ?></h6>
<div><?php echo htmlspecialchars($inform['Last Name']); ?></div>
</div>
<div class="card-action right-align">
<a class="brand-text" href="#">More Info
</div>
</div>
</div>
<?php } ?>
</div>
</div>
<title> Email and Name List </title>
</head>
<body>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Emails</th>
<th>Date Created</th>
</tr>
</table>
</body>
</html>

Output in browser gyazo:

最佳答案

您必须更改 php 中输出所有表的代码,例如:

<body>
<?php


$conn = mysqli_connect('localhost', 'Admin', 'admin1', 'info');

if (!$conn){
echo "Connection failed:" . mysqli_connect_error();
}
//Writing query for database.
$sql = "SELECT `First Name`,`Last Name`,Emails,`Date Created` FROM clientinfo ORDER BY `Date
Created`";

//Querying and getting results

$result = mysqli_query($conn,$sql);

if ($result->num_rows>0){
echo '
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Emails</th>
<th>Date Created</th>
</tr>';
while($row = $result->fetch_assoc()){
echo "<tr> ";
echo "<td>" . $row["First Name"] . "</td>";
echo "<td>" . $row["Last Name"] . "</td>";
echo "<td>" . $row["Date Created"] . "</td>";
echo "</tr> ";
}

echo"</table>";
}
else{
echo "0 result";
}

//Fetch resulting rows as an array

$informed = mysqli_fetch_all($result, MYSQLI_ASSOC);

// Freeing result from the memory.

mysqli_free_result($result);

mysqli_close($conn);

?>

</body>

另一个问题,您确定是 $row["First Name"] 而不是 $row["First_Name"] 吗?
最后一个技巧学习如何准备 stm 来防止 sql 注入(inject)

关于PHP 不将数据库打印为表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59784792/

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