我需要我的电脑游戏从用户的总数中获取所有杀戮和死亡,然后使其杀死除以死亡,然后将总数放在最后。这个比率被称为杀死率,或者说是KDR"。
<?php
// Create connection
$con=mysqli_connect("ipaddress","user","password","minecraft");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT *, `kills`/`deaths` as `KDR` FROM war_kills ``ORDER BY kills DESC");
echo "<table border='1'>
<tr>
<th>Player</th>
<th>Kills</th>
<th>Deaths</th>
<th>KDR</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['player'] . "</td>";
echo "<td>" . $row['kills'] . "</td>";
echo "<td>" . $row['deaths'] . "</td>";
echo "<td>" . $row['KDR'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
到目前为止,我们已经解决了这个问题:http://gexgaming.com/warstats/index.php
答案 0 :(得分:1)
从
更改查询SELECT player, kills, deaths, KDR FROM war_kills ORDER BY kills DESC`
到
SELECT player, kills, deaths, kills / deaths as KDR FROM war_kills ORDER BY kills DESC`
答案 1 :(得分:1)
按照建议修改SQL查询或在PHP循环中执行:
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['player'] . "</td>";
echo "<td>" . $row['kills'] . "</td>";
echo "<td>" . $row['deaths'] . "</td>";
echo "<td>" . ($row['kills'] / $row['deaths']) . "</td>"; // <--- this one
echo "</tr>";
}
答案 2 :(得分:1)
您似乎需要根据玩家名称对结果进行分组。此外,您需要考虑何时deaths = 0
。尝试将您的查询更改为 -
SELECT
player,
SUM(kills) as kills,
SUM(deaths) as deaths,
CASE WHEN SUM(deaths) = 0 THEN SUM(kills)
ELSE SUM(kills)/SUM(deaths)
END as `KDR`
FROM war_kills
GROUP BY player
ORDER BY kills DESC
答案 3 :(得分:0)
我读到的其他答案没有说明某人可能没有死亡的事实。如果他们这样做,你将得到除零误差。
在你的while循环中,
if ($row['deaths'] == 0)
$kdr = '-';
else
$kdr = $row['kills']/$row['deaths'];
然后将$row['KDR']
更改为$kdr