将 SQL 数据库中的数据显示到 php/html 表中
Posted
技术标签:
【中文标题】将 SQL 数据库中的数据显示到 php/html 表中【英文标题】:Display data from SQL database into php / html table 【发布时间】:2018-04-25 09:19:10 【问题描述】:我一直在尝试模仿link 中的解决方案,但没有成功。我的目的是从 SQL 数据库中导入数据并将它们放入一个 html 表中,以便我可以使用 CSS 来修改表的外观。有人可以帮帮我吗?
谢谢。
代码
<?php
$mysqli = NEW mysqli('localhost','username','password','database');
require('/home/database/public_html/wp-load.php');
$id = get_the_ID();
$resultSet = $mysqli->query("SELECT * FROM sweepstake_data WHERE item_id = $id");
if($resultSet->num_rows !=0)
echo "<table>"; // start a table tag in the HTML
while($rows = $resultSet->fetch_assoc())
$description = $rows['description'];
$links = $rows['links'];
$category = $rows['category'];
$eligibility = $rows['eligibility'];
$start_date = $rows['start_date'];
$end_date = $rows['end_date'];
$entry_frequency = $rows['entry_frequency'];
$prizes = $rows['prizes'];
$victory_prizes = $rows['victory_prizes'];
$additional_comments = $rows['additional_comments'];
"<tr><td>" . echo $description != "" ? "<p>Name: $description<br />" : "" ;
"<tr><td>" . echo $links != "" ? "Link: <a href=$links>Click here</a> <br />" : "" ;
"<tr><td>" . echo $category != "" ? "Category: $category<br />" : "" ;
"<tr><td>" . echo $eligibility != "" ? "Eligibility: $eligibility<br />" : "" ;
"<tr><td>" . echo $start_date != "" ? "Start date:$start_date<br />" : "" ;
"<tr><td>" . echo $end_date != "" ? "End date: $end_date<br />" : "" ;
"<tr><td>" . echo $entry_frequency != "" ? "Entry frequency: $entry_frequency<br />" : "" ;
"<tr><td>" . echo $prizes != "" ? "Prizes: $prizes<br />" : "" ;
"<tr><td>" . echo $victory_prizes != "" ? "Victory prizes: $victory_prizes<br />" : "" ;
"<tr><td>" . echo $additional_comments != "" ? "Additional comments: $additional_comments<br />" : "" ;
echo "</table>"; //Close the table in HTML
else
echo "No results.";
?>
【问题讨论】:
我首先建议您了解准备好的和绑定的查询 @Strawberry 肯定会看看。我当然会阅读更多内容,但快速搜索会发现 SQL 注入的危险?您是否暗示我的代码容易受到它的影响?谢谢。 【参考方案1】:使用准备好的查询而不是正常的。
此代码将起作用:
<?php
$mysqli = NEW mysqli('localhost','username','password','database');
require('/home/database/public_html/wp-load.php');
$id = get_the_ID();
$resultSet = $mysqli->prepare("SELECT * FROM sweepstake_data WHERE item_id=?");
$resultSet->bind_param('i',$id);
$resultSet->execute();
$result = $resultSet->get_result();
if($result->num_rows !=0)
echo "<table>"; // start a table tag in the HTML
while($rows = $result->fetch_assoc())
$description = $rows['description'];
$links = $rows['links'];
$category = $rows['category'];
$eligibility = $rows['eligibility'];
$start_date = $rows['start_date'];
$end_date = $rows['end_date'];
$entry_frequency = $rows['entry_frequency'];
$prizes = $rows['prizes'];
$victory_prizes = $rows['victory_prizes'];
$additional_comments = $rows['additional_comments'];
echo "<tr><td>";
$description != "" ? "<p>Name: $description<br />" : "" ;
echo "<tr><td>";
echo $links != "" ? "Link: <a href=$links>Click here</a> <br />" : "" ;
echo "<tr><td>";
echo $category != "" ? "Category: $category<br />" : "" ;
echo "<tr><td>";
echo $eligibility != "" ? "Eligibility: $eligibility<br />" : "" ;
echo "<tr><td>" ;
echo $start_date != "" ? "Start date:$start_date<br />" : "" ;
echo "<tr><td>" ;
echo $end_date != "" ? "End date: $end_date<br />" : "" ;
echo "<tr><td>";
echo $entry_frequency != "" ? "Entry frequency: $entry_frequency<br />" : "" ;
echo "<tr><td>";
echo $prizes != "" ? "Prizes: $prizes<br />" : "" ;
echo "<tr><td>";
echo $victory_prizes != "" ? "Victory prizes: $victory_prizes<br />" : "" ;
echo "<tr><td>";
echo $additional_comments != "" ? "Additional comments: $additional_comments<br />" : "" ;
echo "</table>"; //Close the table in HTML
else
echo "No results.";
?>
【讨论】:
不幸的是,我无法让它工作。感谢您的帮助。【参考方案2】:使用下面的代码。您的问题是打印值
<?php
$mysqli = NEW mysqli('localhost','username','password','database');
require('/home/database/public_html/wp-load.php');
$id = get_the_ID();
$resultSet = $mysqli->query("SELECT * FROM sweepstake_data WHERE item_id = $id");
if($resultSet->num_rows !=0)
echo "<table>"; // start a table tag in the HTML
while($rows = $resultSet->fetch_assoc())
$description = $rows['description'];
$links = $rows['links'];
$category = $rows['category'];
$eligibility = $rows['eligibility'];
$start_date = $rows['start_date'];
$end_date = $rows['end_date'];
$entry_frequency = $rows['entry_frequency'];
$prizes = $rows['prizes'];
$victory_prizes = $rows['victory_prizes'];
$additional_comments = $rows['additional_comments'];
echo "<tr><td>";
$description != "" ? "<p>Name: $description<br />" : "" ;
echo "<tr><td>";
echo $links != "" ? "Link: <a href=$links>Click here</a> <br />" : "" ;
echo "<tr><td>";
echo $category != "" ? "Category: $category<br />" : "" ;
echo "<tr><td>";
echo $eligibility != "" ? "Eligibility: $eligibility<br />" : "" ;
echo "<tr><td>" ;
echo $start_date != "" ? "Start date:$start_date<br />" : "" ;
echo "<tr><td>" ;
echo $end_date != "" ? "End date: $end_date<br />" : "" ;
echo "<tr><td>";
echo $entry_frequency != "" ? "Entry frequency: $entry_frequency<br />" : "" ;
echo "<tr><td>";
echo $prizes != "" ? "Prizes: $prizes<br />" : "" ;
echo "<tr><td>";
echo $victory_prizes != "" ? "Victory prizes: $victory_prizes<br />" : "" ;
echo "<tr><td>";
echo $additional_comments != "" ? "Additional comments: $additional_comments<br />" : "" ;
echo "</table>"; //Close the table in HTML
else
echo "No results.";
?>
【讨论】:
以上是关于将 SQL 数据库中的数据显示到 php/html 表中的主要内容,如果未能解决你的问题,请参考以下文章
UWP 应用程序 - 将 SQL 表中的数据显示到数据网格或列表框
将数据库中的值作为 PHP/HTML 表中的链接回显 [关闭]