如何在html页面中以表格格式输出,使用php脚本并从mysql数据库中提取? [关闭]
问题描述:
I am trying to fetch the data from Mysql database and print it on new web page. I need to display the output in table format.
below is the PHP script, help me out to do so.
Thanks in advance.
<?php
$userinput = $_POST['Sample_name'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ProcessTrackingSystem";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_errno) {
printf("Connect failed: %s
", $conn->connect_error);
exit();
}
$result = mysqli_query($conn, "SELECT * FROM ProcessTrackingSystem.ProcessDetails
WHERE Sample_name = '$userinput'") or die(mysqli_error($conn));
while ($row=mysqli_fetch_assoc($result))
{
printf("Sample_name:->");
printf($row['Sample_name']);
printf("<br>
");
printf("<br>
");
printf("SO_ID:->");
printf($row['SO_ID']);
printf("<br>
");
printf("<br>
");
}
mysqli_free_result($result);
$conn->close();
?>
I need output in tabular format.
答
You can create table as following way.
echo "<table border='1'>
<tr>
<th>Sample_name</th>
<th>SO_ID</th>
</tr>";
while($row = mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $row['Sample_name'] . "</td>";
echo "<td>" . $row['SO_ID'] . "</td>";
echo "</tr>";
}
echo "</table>";
答
It will put in tabular format
<?php
$userinput = $_POST['Sample_name'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ProcessTrackingSystem";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_errno) {
printf("Connect failed: %s
", $conn->connect_error);
exit();
}
$result = mysqli_query($conn, "SELECT * FROM ProcessTrackingSystem.ProcessDetails WHERE Sample_name = '$userinput'") or die(mysqli_error($conn));
echo "<table class='table' border='1' >";
echo "<tr><th>Sample Name</th><th>SO ID</th></tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>{$row['Sample_name']}</td>";
echo "<td>{$row['SO_ID']}</td>";
echo "</tr>";
}
echo "</table>";
mysqli_free_result($result);
$conn->close();
?>