PHP不将数据库打印为表
Posted
技术标签:
【中文标题】PHP不将数据库打印为表【英文标题】:PHP not printing database as table 【发布时间】:2020-01-17 09:45:11 【问题描述】:我正在尝试将 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:
【问题讨论】:
在<table>
之间循环你的数据
【参考方案1】:
您必须更改输出所有表格的代码,如:
<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注入
【讨论】:
这会输出数据,但同样不会以表格格式执行。所有数据都在 First_Name 列下输出,请参阅:gyazo.com/9e409ad09464dde49b9154cf1997a200 这可能是我的 sql 数据库有问题吗?我以为我设置正确了 @SimoneRossaini 您能否准确指出在查询中没有参数时代码如何容易受到 SQL 注入的影响? 您忘记了 echo 循环中的“电子邮件”行,但是是的,一切都很完美!谢谢伙计,这对我有很大帮助。 @symcbean 见here所有参考 @SimoneRossaini 不。我问在 OP 提供的代码中哪里存在 SQLI 漏洞 - 这是一种礼貌的方式,指出您在您的文件中提供了误导性和不相关的信息回答。【参考方案2】:根据您的代码,您正尝试在下面定义实际表之前打印该表。你可以试试这样:
<?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);
$informed = mysqli_fetch_all($result, MYSQLI_ASSOC);
?>
<!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>
<?php
if ($result->num_rows>0)
while($row = $result->fetch_assoc())
echo "<tr><td>" . $row["First Name"] . "</td><td>" . $row["Last Name"] . "</td><td>" . $row["Emails"] . "</td><td>" . $row["Date Created"] . "</td></tr>";
else
echo "<tr><td rowspan=\"5\">0 result</td></tr>";
?>
</table>
</body>
<?php
// Freeing result from the memory.
mysqli_free_result($result);
mysqli_close($conn);
?>
【讨论】:
以上是关于PHP不将数据库打印为表的主要内容,如果未能解决你的问题,请参考以下文章