gpt4 book ai didi

php - 在 PHP 中运行查询以显示在表中

转载 作者:行者123 更新时间:2023-11-30 00:03:32 24 4
gpt4 key购买 nike

我正在尝试创建一个联赛表,该查询在 phpmyadmin 中运行。但我无法使用 php 将其显示在表格中。

我认为问题在于我没有以正确的方式运行查询。当我运行代码时,我得到“联赛表查询未填充”

请您就运行此程序提供一些建议:

    <?php

$sql =
"SELECT player_name,
SUM(win+draw+lose) AS Played,
SUM(win) AS Won,
SUM(draw) AS Drawn,
SUM(lose) AS Lost,
SUM(`for`) AS `Goals For`,
SUM(`against`) AS `Goals Against`,
SUM(cast(`for`AS SIGNED) - cast(`against`AS SIGNED)) AS `Goal Difference`,
SUM((win*3)+(draw)+extra_points) AS Points,
ROUND((SUM((win*3)+draw))/SUM((win+draw+lose)*3)*100,1) AS Record

FROM appearances
WHERE season_id = 4
GROUP BY player_name
ORDER BY Points DESC,
Played ASC,
`Goal Difference` DESC, `Goals For` DESC,
player_name ASC[...]";

if (!$sql) {
echo 'sql query has not worked';

}else {


$leaguetable = mysql_query($sql);
if (!$leaguetable){
echo 'league table query not populated';
} else {

$records = mysql_fetch_array($leaguetable);

If (!$records) {
echo 'records have not been placed in assoc array';
}

else {

echo "<tr width='600'>";
echo "<td>" . $records["player_name"] . "</td>";
echo "<td>" . $records["Played"] . "</td>";
echo "<td>" . $records["Won"] . "</td>";
echo "<td>" . $records["Drawn"] . "</td>";
echo "<td>" . $records["Lost"] . "</td>";
echo "<td>" . $records["Goals For"] . "</td>";
echo "<td>" . $records["Goals Against"] . "</td>";
echo "<td>" . $records["Goal Difference"] . "</td>";
echo "<td>" . $records["Points"] . "</td>";
echo "<td>" . $records["Record"] . "</td>";
echo "</tr>";

//while ($records = mysql_fetch_assoc($sql));
}
}
}
?>

当我运行上面的代码时,我得到“联赛表查询未填充”

预先感谢您的帮助

我刚刚尝试了PDO方法,它仍然没有显示记录,我相信有一个数据库连接,这是代码

 <?php

require 'pdoconnect.php';

$db = new PDO('mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['dbname'], $config['db']['username'], $config['db']['password']);

$stmt = $db->prepare("SELECT player_name,
SUM(win+draw+lose) AS Played,
SUM(win) AS Won,
SUM(draw) AS Drawn,
SUM(lose) AS Lost,
SUM(`for`) AS `Goals For`,
SUM(`against`) AS `Goals Against`,
SUM(cast(`for`AS SIGNED) - cast(`against`AS SIGNED)) AS `Goal Difference`,
SUM((win*3)+(draw)+extra_points) AS Points,
ROUND((SUM((win*3)+draw))/SUM((win+draw+lose)*3)*100,1) AS Record

FROM appearances
WHERE season_id = 4
GROUP BY player_name
ORDER BY Points DESC,
Played ASC,
`Goal Difference` DESC, `Goals For` DESC,
player_name ASC[...]");

$stmt->execute();
$result = $stmt->fetchAll(pdo::FETCH_ASSOC);

while($row = $result){
echo "<tr width='600'>";
echo "<td>" . $row['player_name'] . "</td>";
echo "<td>" . $row['Played'] . "</td>";
echo "<td>" . $row['Won'] . "</td>";
echo "<td>" . $row['Drawn'] . "</td>";
echo "<td>" . $row['Lost'] . "</td>";
echo "<td>" . $row['Goals For'] . "</td>";
echo "<td>" . $row['Goals Against'] . "</td>";
echo "<td>" . $row['Goal Difference'] . "</td>";
echo "<td>" . $row['Points'] . "</td>";
echo "<td>" . $row['Record'] . "</td>";
echo "</tr>";

//while ($records = mysql_fetch_assoc($sql));
}
?>

最佳答案

请替换此语句

$leaguetable = mysql_query($sql);    

$leaguetable = mysql_query($sql) or die(mysql_error());    

检查查询失败的原因

关于php - 在 PHP 中运行查询以显示在表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24802431/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com