简体   繁体   中英

Display SQL query in a HTML table

I have some SQL query code here where if I put it into PHPMyAdmin in the correct database it displays what I want to see, but I want it to display in an HTML table, any idea?

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 ; 

Currently when I run this code in PHPMyAdmin this is the result I get and this is what I want to output in a html table:

截图

I'm trying to create this off this website:

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

So far I have tried to run a query but nothings coming up;

        <?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();
?>

Well, you can do something like this

<?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>";
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM