2012-05-01 156 views
4

SQL/PHP查询适用于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> 
+0

你会得到什么错误? –

+2

你的意思是“作品”和“不”。你看到什么错误信息,副作用等?请删除不相关的代码,以便我们可以专注于真正的问题。 –

+0

'WHERE名称LIKE错误()%'可能是多数民众赞成在问题?它对我没有意义 –

回答

4

我不知道MySQL,但是我知道,在SQL当你做一个聚合函数,如SUM(Kills),那么你就无法通过$行引用该行[“杀死”] 。我不知道这是否是您的问题,但您可以尝试在您的SELECT语句中将SUM(Kills)视为'杀死'。这样做你的聚合SELECTs将允许你以这种方式引用它们。

+0

必须是它。他需要做'杀死'杀死'等等,每列可以做$ row [杀死] –

+0

我会尝试这个并在接下来的几分钟后回来。 – Andrew

+0

谢谢!分配别名是正确的答案。 – Andrew

相关问题