sql/php查询在phpmyadmin中工作,但在站点中不工作。
我注意到很多人都遇到过这个问题,但不可否认的是,我还没有这个网站上的一些程序员先进。=)我谦恭地请求您提供任何可能的经验:p谢谢。

<?php
// session_start();
// ob_start();

ini_set("display_errors", true);
error_reporting(-1);

// Connection to database.
mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db('') or die(mysql_error());
?>

<?php
// Maintenance page.

$maintenance = 1; // 1 = Site Online || 0 = Site Offline

if ($maintenance == 0) {
    ?>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <head>
            <style type="text/css">
                body {
                    color:#ffffff;
                    background-color: #000000;
                    font-family: Arial, Helvetica, sans-serif;
                }
            </style>
        </head>
        <title></title>
    </head>
    <body>
    <center><img src="images/p4flogo.png" /></center><br />
    <?php
    echo "<br/><br /><center>This site is currently under maintenance.</center>";
} else {

// Start of main website.
    ?>
<html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <head>
            <style type="text/css">
                body {
                    color:#ffffff;
                    background-color: #000000;
                    font-family: Arial, Helvetica, sans-serif;
                }
                a:link {color: orange; text-decoration: underline; }
                a:active {color: red; text-decoration: underline; }
                a:visited {color: orange; text-decoration: underline; }
                a:hover {color: red; text-decoration: none; }
                table {
                    border-color: #333333;
                }
                th {
                    background-color:#ffffff;
                    color:#000000;
                    font-family: Arial, Helvetica, sans-serif;
                    height:30px;
                }
                td { font-family: Arial, Helvetica, sans-serif;
                     color:#ffffff;
                     height:35px;
                }
            </style>
        </head>
        <title></title>
    </head>
    <body>
        <table border="0" cellspacing="1" align="center">
            <tr><td>
            <center><img src="images/p4flogo.png" /></center><br /><br />
            <form action="" method="post">
                Search for a soldier: <input type="text" name="value" size="35" /><input type="submit" value="search" /><br />
                <?php
                if (isset($_POST['value']) && !empty($_POST['value'])) {
                    $value = mysql_real_escape_string($_POST['value']);
                    // query to database for soldier stats
                    // query works in phpmyadmin but not on site.
                         $sql = "
                               SELECT
                                `Name`,
                                 MAX(`Level`),
                                `Class`,
                                 SUM(`Kills`),
                                 SUM(`Deaths`),
                                 SUM(`Points`),
                                 SUM(`TotalTime`),
                                 SUM(`TotalVisits`),
                                 `CharacterID`
                               FROM
                                 `Characters`
                               WHERE
                                 `Name` LIKE '$value%' OR `CharacterID` LIKE '$value'
                               GROUP BY
                                 `Name`,
                                 `Class`,
                                 `CharacterID`
                               ORDER BY
                                 `Name` ASC;";
                    $query = mysql_query($sql);
                    $numrow = mysql_num_rows($query);
                    if ($numrow >= 1) {
                        echo "<br /><a href=\"index.php\"><b>View TOP 100 Players!</b></a><br />";
                        echo "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">  ";
                        echo "<th>Soldier Name</th><th>Level</th><th>Class</th><th>KDR</th><th>Kills</th><th>Deaths</th><th>Points</th><th>Hours Played</th><th>Total Visits</th><th>CharacterID</th>";

                        echo "<br />";
                        WHILE ($row = mysql_fetch_assoc($query)) {

                            $SoldierName = $row['Name'];
                            $Level = $row['Level'];
                            $Class = $row['Class'];
                            if ($Class == NULL | empty($Class)) {
                                $Class = '<center>n / a</center>';
                            }
                            if ($Class == 1) {
                                $Class = 'Assault';
                            }
                            if ($Class == 2) {
                                $Class = 'Recon';
                            }
                            if ($Class == 3) {
                                $Class = 'Medic';
                            }
                            if ($Class == 4) {
                                $Class = 'Engineer';
                            }
                           echo $Kills = $row['Kills'];
                              if ($Kills == 0) {
                                  $Kills = 1;
                              }
                            $Deaths = $row['Deaths'];
                            if ($Deaths == 0) {
                                $Deaths = 1;
                            }
                            $Kdr = round($Kills / $Deaths, 2);
                            $Points = $row['Points'];

                            $TimePlayed = $row['TotalTime'];
                            if ($TimePlayed == 0) {
                                $TimePlayed = 1;
                            } else {
                                $TimePlayed = round(($TimePlayed / 3600), 0);
                            }

                            $TotalVisits = $row['TotalVisits'];
                            $CharacterID = $row['CharacterID'];

                            echo "<tr>";
                            echo "<td><b>$SoldierName</b></td>";
                            echo "<td>$Level</td>";
                            echo "<td>$Class</td>";
                            if ($Kdr > 3.9) {
                                echo "<td><font color=\"red\"><b>$Kdr</b></font></td>";
                            } else if ($Kdr > 2.5 && $Kdr < 4) {
                                echo "<td><font color=\"orange\"><b>$Kdr</b></font></td>";
                            } else {
                                echo "<td><font color=\"limegreen\">$Kdr</font></td>";
                            }
                            echo "<td>$Kills</td>";
                            echo "<td>$Deaths</td>";
                            echo "<td>$Points</td>";
                            echo "<td>$TimePlayed</td>";
                            echo "<td>$TotalVisits</td>";
                            echo "<td>$CharacterID</td>";
                            echo "</tr>";
                        }
                        echo "</table>";
                    } else {
                        echo "No player found with that name. Please try again.";
                    }
                } else {
                    if (empty($_POST['value'])) {
                        echo "<font color=\"red\">You must enter a search value.</font>";
                    }
                    // query to p4f database for top 100 players.
                    $sql = "SELECT * FROM `Characters` WHERE `Points` > 1 GROUP BY `Name` ORDER BY `Points` DESC LIMIT 100;";
                    $query = mysql_query($sql);
                    echo "<h3>TOP 100 PLAYERS</h3>";
                    echo "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">  ";
                    echo "<th></th><th>Soldier Name</th><th>Level</th><th>Class</th><th>KDR</th><th>Kills</th><th>Deaths</th><th>Points</th><th>Hours Played</th><th>Total Visits</th><th>CharacterID</th>";
                    // echo "Made it to loop!";
                    $Rank = 1;
                    WHILE ($row = mysql_fetch_assoc($query)) {

                        $SoldierName = $row['Name'];
                        $Level = $row['Level'];
                        $Class = $row['Class'];
                        if ($Class == NULL | empty($Class)) {
                                $Class = '<center>n / a</center>';
                            }
                        if ($Class == 1) {
                            $Class = 'Assault';
                        }
                        if ($Class == 2) {
                            $Class = 'Recon';
                        }
                        if ($Class == 3) {
                            $Class = 'Medic';
                        }
                        if ($Class == 4) {
                            $Class = 'Engineer';
                        }
                        $Kills = $row['Kills'];
                        if ($Kills == 0) {
                            $Kills = 1;
                        }
                        $Deaths = $row['Deaths'];
                        if ($Deaths == 0) {
                            $Deaths = 1;
                        }
                        $Kdr = round($Kills / $Deaths, 2);
                        $Points = $row['Points'];

                        $TimePlayed = $row['TotalTime'];
                        if ($TimePlayed == 0) {
                            $TimePlayed = 1;
                        } else {
                            $TimePlayed = round(($TimePlayed / 3600), 0);
                        }

                        $TotalVisits = $row['TotalVisits'];
                        $CharacterID = $row['CharacterID'];

                        echo "<tr>";
                        echo "<td>$Rank</td>";
                        echo "<td><b>$SoldierName</b></td>";
                        echo "<td>$Level</td>";
                        echo "<td>$Class</td>";
                        if ($Kdr > 3.9) {
                            echo "<td><font color=\"red\"><b>$Kdr</b></font></td>";
                        } else if ($Kdr > 2.5 && $Kdr < 4) {
                            echo "<td><font color=\"orange\"><b>$Kdr</b></font></td>";
                        } else {
                            echo "<td><font color=\"limegreen\">$Kdr</font></td>";
                        }
                        echo "<td>$Kills</td>";
                        echo "<td>$Deaths</td>";
                        echo "<td>$Points</td>";
                        echo "<td>$TimePlayed</td>";
                        echo "<td>$TotalVisits</td>";
                        echo "<td>$CharacterID</td>";
                        echo "</tr>";
                        $Rank++;
                    }
                    echo "</table>";
                }
            }
            ?>
            </td></tr>
    </table>
</body>
</html>

最佳答案

我对mysql不太确定,但我知道在sql中,当您执行sum(Kills)之类的聚合函数时,就不能通过$row['kills'引用行。我不知道这是否是您的问题,但您可以尝试在select语句中将sum(Kills)作为“kills”。对聚合选择执行此操作将允许您一直引用它们。

10-07 22:33