显示两个PHP变量之间的十进制比率

时间:2015-01-04 13:24:04

标签: mysql function phpunit

我记录我的Minecraft服务器的统计数据并在我的网站上显示玩家的游戏统计数据。我已经记录了死亡和死亡事件,但现在我试图获得有效的死亡/死亡率。

我试图以十进制比例格式显示杀戮/死亡(例如:3789杀死 - 5711死亡会给你一个0.663的K / DR)

elseif ($_GET['task'] == 'stats') {
        $get_player = $_GET['player'];
        $get_db = 'engine';
        $result = mysql_query("SELECT * FROM $get_db WHERE name = '" . mysql_real_escape_string($get_player) . "'", $link);
            while($data = mysql_fetch_array($result)) {
                echo '{"task":"viewstats","kills":"'; echo $data['kills'];
                                echo '","deaths":"'; echo $data['deaths'];
                                echo '","joins":"'; echo $data['joins'];
                                echo '","quits":"'; echo $data['quits'];
                                echo '","kicked":"'; echo $data['kicked'];
                echo '"}';
            }
    }

我在这样的表格中呼吁他们:

<td><?php echo empty($stats) ? "--" : substr($stats->kills, 0, 50); ?></td>
      <td><?php echo empty($stats) ? "--" : substr($stats->deaths, 0, 50); ?></td>

上面的PHP代码是一个API文件,MySQL已经在其中启用了 - 我只发布了一段API。

2 个答案:

答案 0 :(得分:1)

你可以这样做:

echo json_encode(array(
    'task' => 'viewstats',
    'kills' => $data['kills'],
    'deaths' => $data['deaths'],
    'joins'=> $data['joins'],
    'quits' => $data['quits'],
    'kicked' => $data['kicked'],

    // then ratio
    'ratio' => $data['kills'] / $data['deaths'],
));

答案 1 :(得分:0)

//**Make sure this Function is declared at the top of your script.**
function MySQLi_quickConnect()
{
  $host     = 'somewebsite.db.120327161.hostedresource.com'; //or 'http://localhost'
  $username = '<YOUR USERNAME>';
  $password = '<YOUR PASSWORD>';
  $database = '<YOUR DATABASES NAME>';

  $db = new MySQLi($host,$username,$password,$database);
  $error_message = $db->connect_error;
  if($error_message != NULL){die("Error:" . $error_message . "<br>" . "Occured in function
                                         MySQLi_quickConnect");}
  return $db;   
}

//Replace your code with this: 

 elseif($_GET['task'] == 'stats') {
 $get_player = $_GET['player'];
 $get_db = 'engine';

 $mysqli = MySQLi_quickConnect();
 $query  = ('SELECT kills, deaths, FROM ? WHERE name = ? ');
 if ($stmt = $mysqli->prepare($query)) {
  $stmt->bind_param("ss", $get_db, $get_player);
  $stmt->execute();
  $stmt->bind_result($kills, $deaths);
 }
 while ($stmt->fetch()) {
  $kdr = $kills/$deaths;
  echo "You have a  K/DR of " . $kdr . "<br>";
 }

 $stmt->close();
}

注意:验证数据库连接,表名和$ _Get变量。