目前我有一个链接到MySQL数据库的表,它得到了数据库中的行。
它完美运行,数组显示正常。
但是,不是每次都显示一张普通的桌子,而是像下面一样并排显示它。
由于隐私,播放器名称已被删除。但是空白的空间是玩家所在的位置。
这是数据库连接文件:
<?php
// MySQL Connection Details
$mysql_host = ""; // Host name
$mysql_username = ""; // Mysql username
$mysql_password = ""; // Mysql password
$mysql_database = ""; // Database name
$mysql_table = ""; // Table name
// MySQL Connection
$con = mysqli_connect("$mysql_host","$mysql_username","$mysql_password","$mysql_database");
// MySQL Error Logging
if (mysqli_connect_errno())
{
echo "MySQL Connection Failed: " . mysqli_connect_error();
}
$sql = "SELECT * FROM $mysql_table";
$result = $con->query($sql);
?>
以下是它在主php文件中的显示方式。
<table id="ldr_table">
<tr class="lb_tb_hd" align="center">
<td>Player</td>
<td>Points</td>
<td>Wins</td>
<td>Kills</td>
<td>Deaths</td>
<td>Played</td>
</tr>
<tr class="ldr_alt">
<?php
require('{db connection file}');
while($row = mysqli_fetch_array($result, MYSQLI_BOTH))
{
echo '<td><center>' . $row['player_name'] . '</center></td>';
echo '<td><center>' . $row['score'] . '</center></td>';
echo '<td><center>' . $row['games_won'] . '</center></td>';
echo '<td><center>' . $row['kills'] . '</center></td>';
echo '<td><center>' . $row['deaths'] . '</center></td>';
echo '<td><center>' . $row['games_played'] . '</center></td>';
}
mysqli_close($con);
?>
</tr>
</table>
我尝试了几件事,包括foreach
,这对格式没有影响。
如果还有其他文件,我很乐意在这里展示。
答案 0 :(得分:1)
您需要为每个SQL结果创建一个新行
<?php
require('{db connection file}');
while($row = mysqli_fetch_array($result, MYSQLI_BOTH))
{
echo '<tr class="ldr_alt">';
echo '<td><center>' . $row['player_name'] . '</center></td>';
echo '<td><center>' . $row['score'] . '</center></td>';
echo '<td><center>' . $row['games_won'] . '</center></td>';
echo '<td><center>' . $row['kills'] . '</center></td>';
echo '<td><center>' . $row['deaths'] . '</center></td>';
echo '<td><center>' . $row['games_played'] . '</center></td>';
echo '</tr>';
}
mysqli_close($con);
?>
PS:使用
text-align: center;
而不是多重中心。
答案 1 :(得分:0)
循环中回显<tr>
标记。
while($row = mysqli_fetch_array($result, MYSQLI_BOTH))
{
echo '<tr class="ldr_alt">';
echo '<td><center>' . $row['player_name'] . '</center></td>';
echo '<td><center>' . $row['score'] . '</center></td>';
echo '<td><center>' . $row['games_won'] . '</center></td>';
echo '<td><center>' . $row['kills'] . '</center></td>';
echo '<td><center>' . $row['deaths'] . '</center></td>';
echo '<td><center>' . $row['games_played'] . '</center></td>';
echo '</tr>';
}
答案 2 :(得分:0)
。在while循环中放置<tr>
标记
<table id="ldr_table">
<tr class="lb_tb_hd" align="center">
<td>Player</td>
<td>Points</td>
<td>Wins</td>
<td>Kills</td>
<td>Deaths</td>
<td>Played</td>
</tr>
<?php
require('{db connection file}');
while($row = mysqli_fetch_array($result, MYSQLI_BOTH))
{
echo '<tr class="ldr_alt">'
echo '<td><center>' . $row['player_name'] . '</center></td>';
echo '<td><center>' . $row['score'] . '</center></td>';
echo '<td><center>' . $row['games_won'] . '</center></td>';
echo '<td><center>' . $row['kills'] . '</center></td>';
echo '<td><center>' . $row['deaths'] . '</center></td>';
echo '<td><center>' . $row['games_played'] . '</center></td>';
echo '</tr>'
}
mysqli_close($con);
?>
</table>