使用带有多个语句的PHP / SQL填充表

时间:2017-01-06 21:52:00

标签: php mysql sql

我似乎很难找到一个搜索栏来显示PHP / SQL搜索的结果并用它们填充表格。原因是我不习惯首先创建表所需的SQL数量。这是PHP脚本。

    // Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
}


if(isset($_GET['statsearch'])) {

$statSearch = $_GET['statsearch'];

}

if (!empty($statSearch)) {
    $sql = "SELECT player, zeus_stats_player.name, SUM(KILLS) AS TOTALKILLS, SUM(DEATHS) AS TOTALDEATHS,SUM(KILLS)/IF(SUM(DEATHS) > 0,SUM(DEATHS),1)  AS KDR
FROM (
SELECT player, 0 AS KILLS, sum(count) AS DEATHS FROM zeus_stats_player_death WHERE Cause <> 'SUICIDE' GROUP BY player
UNION ALL
SELECT killer as player, COUNT(1) AS KILLS, 0 AS DEATHS FROM zeus_stats_player_kill group by killer) AS TBL INNER JOIN zeus_stats_player ON zeus_stats_player.id = TBL.player
GROUP BY zeus_stats_player.name WHERE zeus_stats_player.name like '%$statSearch%'
ORDER BY SUM(KILLS)/IF(SUM(DEATHS) > 0,SUM(DEATHS),1) DESC";
}

$sql = "SELECT player, zeus_stats_player.name, SUM(KILLS) AS TOTALKILLS, SUM(DEATHS) AS TOTALDEATHS,SUM(KILLS)/IF(SUM(DEATHS) > 0,SUM(DEATHS),1)  AS KDR
FROM (
SELECT player, 0 AS KILLS, sum(count) AS DEATHS FROM zeus_stats_player_death WHERE Cause <> 'SUICIDE' GROUP BY player
UNION ALL
SELECT killer as player, COUNT(1) AS KILLS, 0 AS DEATHS FROM zeus_stats_player_kill group by killer) AS TBL INNER JOIN zeus_stats_player ON zeus_stats_player.id = TBL.player
GROUP BY zeus_stats_player.name
ORDER BY SUM(KILLS)/IF(SUM(DEATHS) > 0,SUM(DEATHS),1) DESC LIMIT 15";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    echo "<table class='table'><tr><th>Name</th><th>Kills</th><th>Deaths</th><th>K/D Ratio</th></tr>";
    while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["name"]."</td><td>".$row["TOTALKILLS"]."</td><td>".$row["TOTALDEATHS"]."</td><td>".$row["KDR"]."</td></tr>";
}
echo "</tbody>";
echo "</table>";
} else {
echo "0 results";
}
?>
</div>

<?php
$conn->close();
?>

我想在没有搜索的情况下正常显示结果,这就是底层SQL的内容,但我希望顶部显示结果WHERE playername = searchbox entry。

这是表格的链接。 Table Link

0 个答案:

没有答案