几周前尝试做一个教程并遇到一些麻烦。
我试图从mysql内部的数组中检索所有数据。到目前为止,这是我的代码,哪种方式有效。
include('db_conn.php'); //db connection
$query = "SELECT * FROM kit202_product";
$result = $mysqli->query($query);
while ($row = mysqli_fetch_array($result) )
{
printf("%s %s %d %s\n", $row[0], $row[1],$row[2], $row[3]);
}
该表格看起来像this
虽然代码确实有效(它打印出每个值),但我如何组织它以使它看起来像图片中的那样? 我猜我需要以某种方式打印到桌子上?
谷歌谷歌并没有真正让我到任何地方。只是真正展示了如何打印数组的一行。答案 0 :(得分:0)
include('db_conn.php'); //db connection
$query = "SELECT * FROM kit202_product";
$result = $mysqli->query($query);
echo "<table>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "<th>Price</th>";
echo "<th>Description</th>";
echo "</tr>";
while ($row = mysqli_fetch_array($result) )
{
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "</tr>";
}
echo "</table>";
使用CSS设计表格
答案 1 :(得分:0)
<?php
include('db_conn.php'); //db connection
$query = "SELECT * FROM kit202_product";
$result = $mysqli->query($query);
?>
<TABLE BORDER="5" WIDTH="50%" CELLPADDING="4" CELLSPACING="3">
<TR>
<TH COLSPAN="2"><BR><H3>Search Product Detail</H3>
</TH>
</TR>
<TR>
<TH>ID</TH>
<TH>Name</TH>
<TH>Price</TH>
<TH>Description</TH>
</TR>
<?php
while ($row = mysqli_fetch_array($result) )
{
echo "<TR ALIGN='CENTER'>
<TD>$row[0]</TD>
<TD>$row[1]</TD>
<TD>$row[2]</TD>
<TD>$row[3]</TD>
</TR>";
}
?>
</TABLE>