在PHP中运行Query以在表中显示

时间:2014-07-17 11:35:30

标签: php html mysql sql pdo

我正在尝试创建一个联赛表,该查询在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));
                }
        ?>

4 个答案:

答案 0 :(得分:0)

在向其发出任何查询之前,您必须连接到您的数据库:

在脚本开头尝试此操作(将localhost,mysql_user和mysql_password替换为您的服务器数据):

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
  die('Could not connect: ' . mysql_error());
}

$db_link= mysql_select_db('database_name', $link);
if (!$db_link) {
  die ('Can\'t use database_name: ' . mysql_error());
}

请参阅http://php.net/manual/en/function.mysql-connect.php

http://php.net/manual/en/function.mysql-select-db.php了解更详细信息。

答案 1 :(得分:0)

请替换此声明

$leaguetable = mysql_query($sql);    

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

检查查询失败的原因

答案 2 :(得分:0)

不是真正的andwer,我不知道您使用的是哪个版本的PHP,但您可能会考虑http://php.net/manual/en/function.mysql-query.php上的警告

自PHP 5.5.0起,此扩展程序已弃用,将来将被删除。相反,应该使用MySQLi或PDO_MySQL扩展。

答案 3 :(得分:0)

你可以做这样的事情......

<!doctype html>
<html>
   <head></head>
    <body>      
        <table>
            <tr>
            <?php
            header('Content-Type: text/html; charset=UTF-8'); 
            mysql_connect("localhost", "user", "password");
            mysql_select_db("alarm");

            mysql_query( "SET NAMES utf8" );
            mysql_query( "SET CHARACTER SET utf8" );

            $dave = mysql_query("select blablabla as ab");

            while ($row = mysql_fetch_assoc($dave)) {

                echo $row['ab'];
                echo "<td>";
                .
                . put where you want..
                .
                echo "<tr>";
            }
            ?>

    </body>
</html>