0
$getKills = mysqli_query($connection, "SELECT * FROM Kills");
$getMurderer = mysqli_query($connection, "SELECT * FROM Kills WHERE Killer='$getUser->Username'");
$numKills = mysqli_num_rows($getKills);
$numKiller = mysqli_num_rows($getMurderer);

How would I get the percentage of how many kills the Player got out of all of them?

e.g. If there was 20 kills recorded, and the player only got 6, it would say

Player has 30% kills out of $numKills

slixxed
  • 15
  • 7

3 Answers3

0

Try this

$getKills = mysqli_query($connection, "SELECT * FROM Kills");
$getMurderer = mysqli_query($connection, "SELECT * FROM Kills WHERE Killer='$getUser->Username'");

$getKillsCount = count($getKills);
$getMurdererCount = count($getMurderer);

if(empty($getKillsCount) || empty($getMurdererCount))
{
    echo 'One of Count is Empty';
}
else
{
    $precentage = ($getKillsCount/$getMurdererCount)*100;
    echo $precentage;
}
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
0

Usually, you can do it like this:

SELECT table.*, COUNT(*) / Table.total * 100

Or just use PHP and do the math:

$percentage = ($playerKills / $allKills) * 100;
0

Try This,

    SELECT 
              CONCAT(
                      ROUND(COUNT(*) /
                           (SELECT COUNT(*) FROM Kills)*100,2)
                     ,'%') 
            AS pcr_kills,
              (SELECT COUNT(*) FROM Kills) 
            AS tot_kills
    FROM 
            Kills 
    WHERE 
            Killer='$getUser->Username';

However, It is recommended to do this process at application side ! There would be worthless burden on the server if the < Kills > table is too large.

Hytool
  • 1,358
  • 1
  • 7
  • 22