我做了這個代碼將數據寫入到一個HTML表格:如何使用PHP將數據寫入多個HTML表格列?
<?php
//DB Verbindung
$con = mysqli_connect("localhost","root","","altislife");
$header = -1;
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT * FROM players";
if ($result=mysqli_query($con,$sql))
{
echo "<thead><tr>";
while ($fieldinfo=mysqli_fetch_field($result))
{
echo "<th>$fieldinfo->name</th>";
$header++;
echo "$header";
}
echo "</tr></thead>";
mysqli_free_result($result);
}
if ($result=mysqli_query($con,$sql))
{
echo "<tbody>";
for ($i=0; $i < $header; $i++) {
while($sql = mysqli_fetch_array($result))
{
echo "<tr><td>" . $sql[$i] . "</td><td> ";
}
}
echo "</tbody>";
}
mysqli_close($con);
的問題是,它只是填充的第一列。
然後我試圖這樣:
<?php
//DB Verbindung
$con = mysqli_connect("localhost","root","","altislife");
$header = -1;
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT * FROM players";
if ($result=mysqli_query($con,$sql))
{
echo "<thead><tr>";
while ($fieldinfo=mysqli_fetch_field($result))
{
echo "<th>$fieldinfo->name</th>";
$header++;
echo "$header";
}
echo "</tr></thead>";
mysqli_free_result($result);
}
if ($result=mysqli_query($con,$sql))
{
echo "<tbody>";
while($sql = mysqli_fetch_array($result))
{
for ($i=0; $i < $header; $i++) {
echo "<tr><td>" . $sql[$i] . "</td><td> ";
}
}
echo "</tbody>";
}
mysqli_close($con);
與此代碼的問題是相同的,與第一個例子。 任何人都可以看到我的錯誤?
您的問題很難理解。你應該更詳細地解釋你的代碼和問題。 – Anselm