我有我的表的循环问题时显示这里是代码
<html>
<?php
$Candidate =$_POST ['candidate'];
$link = mysqli_connect('localhost', 'root', '', 'test') or die(mysqli_connect_error());
$query = "SELECT * FROM `table 1` WHERE `fullname` LIKE '$Candidate%'";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
mysqli_close($link);
$row=mysqli_fetch_assoc($result);
while ($row = mysqli_fetch_array($result))
{
echo <table>
echo "Name Of Candidate:". @$row['fullname'];
echo "<br>";
echo "comments:".@$row['comments'];
}
?>
最初我希望搜索结果以表格格式显示,有什么帮助吗?
您可以尝试以下操作
echo "<table>";
while ($row = mysqli_fetch_array($result))
{
echo "<TR><TD>Name Of Candidate:" . $row['fullname'] . "</td>";
echo "<TD>comments:" . $row['comments'] . "</TD></TR>";
}
echo "</table>";
首先,你的代码容易受到MySQL注入攻击。看这篇文章
说到表的呈现,下面的代码应该可以做得很好:
$table = "<table>'n";
$tableHead = <<<THEAD
<thead>'n
<tr>'n
<th>Name of candidate</th>'n
<th>Comments</th>'n
</tr>'n
</thead>'n
THEAD;
//Add table head
$table .= $tableHead;
while ($row = mysqli_fetch_array($result)) {
//No need for @ before $row, since your table will have those columns?
$tableRow = <<<TABLEROW
<tr>'n
<td>{$row['fullname']}</td>'n
<td>{$row['comments']}</td>'n
</tr>'n
TABLEROW;
$table .= $tableRow;
}
//Close the table
$table .= "</table>'n";
//Print the table
echo $table;