I made this code write data into an HTML table:
<?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);
The problem is that it only fills the first column.
Then I tried this:
<?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);
The problem with this code is the same as with the first example. Can anyone see my mistake?