[英]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? 我在这里有一些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 ;
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: 当前,当我在PHPMyAdmin中运行此代码时,这是我得到的结果,这就是我要在html表中输出的内容:
I'm trying to create this off this website: 我正在尝试通过此网站创建此文件:
http://www.artfulsoftware.com/infotree/qrytip.php?id=804 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>";
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.