在HTML表中显示SQL查询

时间:2019-01-04 09:57:17

标签: php html mysql

我这里有一些SQL查询代码,如果我将其放入正确数据库中的PHPMyAdmin中,它会显示我想要查看的内容,但是我希望它显示在HTML表中,有什么主意吗?

SELECT  
  tname AS Team, Sum(P) AS P,Sum(W) AS W,Sum(D) AS D,Sum(L) AS L, 
  SUM(F) as F,SUM(A) AS A,SUM(GD) AS GD,SUM(Pts) AS Pts  
FROM( 
  SELECT  
    hteam Team,  
    1 P, 
    IF(hscore > ascore,1,0) W, 
    IF(hscore = ascore,1,0) D, 
    IF(hscore < ascore,1,0) L, 
    hscore F, 
    ascore A, 
    hscore-ascore GD, 
    CASE WHEN hscore > ascore THEN 3 WHEN hscore = ascore THEN 1 ELSE 0 END PTS 
  FROM games 
  UNION ALL 
  SELECT  
    ateam, 
    1, 
    IF(hscore < ascore,1,0), 
    IF(hscore = ascore,1,0), 
    IF(hscore > ascore,1,0), 
    ascore, 
    hscore, 
    ascore-hscore GD, 
    CASE WHEN hscore < ascore THEN 3 WHEN hscore = ascore THEN 1 ELSE 0 END 
  FROM games 
) as tot 
JOIN teams t ON tot.Team=t.id  
GROUP BY Team  
ORDER BY SUM(Pts) DESC ; 

当前,当我在PHPMyAdmin中运行此代码时,这是我得到的结果,这就是我想在html表中输出的结果:

Screenshot

我正试图在此网站上创建它:

http://www.artfulsoftware.com/infotree/qrytip.php?id=804

到目前为止,我已经尝试运行查询,但是什么都没有;

        <?php

    $servername = "-";
    $username = "-";
    $password = "-";
    $dbname = "-";

    // Create connection
$mysqli = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
} 
$query = "SELECT  
  tname AS Team, Sum(P) AS P,Sum(W) AS W,Sum(D) AS D,Sum(L) AS L, 
  SUM(F) as F,SUM(A) AS A,SUM(GD) AS GD,SUM(Pts) AS Pts  
FROM( 
  SELECT  
    hteam Team,  
    1 P, 
    IF(hscore > ascore,1,0) W, 
    IF(hscore = ascore,1,0) D, 
    IF(hscore < ascore,1,0) L, 
    hscore F, 
    ascore A, 
    hscore-ascore GD, 
    CASE WHEN hscore > ascore THEN 3 WHEN hscore = ascore THEN 1 ELSE 0 END PTS 
  FROM games 
  UNION ALL 
  SELECT  
    ateam, 
    1, 
    IF(hscore < ascore,1,0), 
    IF(hscore = ascore,1,0), 
    IF(hscore > ascore,1,0), 
    ascore, 
    hscore, 
    ascore-hscore GD, 
    CASE WHEN hscore < ascore THEN 3 WHEN hscore = ascore THEN 1 ELSE 0 END 
  FROM games 
) as tot 
JOIN teams t ON tot.Team=t.id  
GROUP BY Team  
ORDER BY SUM(Pts) DESC ; ";

if ($stmt = $mysqli->prepare($query)) {

    /* execute statement */
    $stmt->execute();



    /* close statement */
    $stmt->close();
}

/* close connection */
$mysqli->close();
?>

1 个答案:

答案 0 :(得分:0)

好吧,你可以做这样的事情

<?php

$connection=mysqli_connect(your database parameters);
$query="sql query";
$r=mysqli_query($connection,$query);
$resultset=array();  //Associative Array
echo "<div id='table'><center><table border=1>
<tr>
<th>Column Headings</th>
<tr>
</tr></center>";    
while($row=mysqli_fetch_assoc($r))
{
    echo "<tr>";
    echo "<td>" . $row['team'] . "</td>";
    echo "<td>" . $row['p'] . "</td>";
    ...
    echo "</tr>";
}
echo "</table><br>";
?>