SQL / PHP查询在PHPmyAdmin中有效,但在站点中无效

时间:2012-05-01 03:33:28

标签: php sql phpmyadmin

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>

1 个答案:

答案 0 :(得分:4)

我不确定MySQL,但我知道在SQL中执行聚合函数时,如SUM(Kills),那么你不能通过$ row ['kills']引用行。我不知道这是不是你的问题,但你可以尝试在SELECT语句中将SUM(Kills)作为'kills'。为聚合SELECT执行此操作将允许您以这种方式引用它们。