0
votes

I just started a game server and made a simple leaderboard on my website where it shows players stats. The problem I'm facing is that it will show duplicate rows in the table.

Here's what I mean:

enter image description here

And here is my code.

<?php        
mysql_connect('','','') or die(mysql_error());
mysql_select_db('dbname')  or die(mysql_error());
$query=mysql_query("select * from playerstats,playerinfo limit 0,10")  or die(mysql_error());
echo'<table border="1" ><th >NOM DU JOEUR</th><th>VICTIMES</th><th>AI TUER</th><th>TEAMKILLS</th><th>DECES</th><th>RÉANIMATIONS</th><th>ARGENT</th>';
while($res=mysql_fetch_array($query))
{
  echo'<tr><td>'.$res['Name'].'</td><td>'.$res['PlayerKills'].'</td><td>'.$res['AIKills'].'</td><td>'.$res['TeamKills'].'</td><td>'.$res['DeathCount'].'</td><td>'.$res['ReviveCount'].'</td><td>'.$res['BankMoney'].'</td></tr>';
}
echo'</table>';
?>

I would apreciate some help. I don't want duplicate entries if the username is already listed.

EDIT: Here is the 2 tables i am trying to fetch from

Here is the 2 tables i am trying to fetch from

-- -----------------------------------------------------
-- Table `PlayerStats`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `PlayerStats` (
  `PlayerUID` VARCHAR(32) NOT NULL,
  `LastModified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `PlayerKills` INT UNSIGNED NOT NULL DEFAULT 0,
  `AIKills` INT UNSIGNED NOT NULL DEFAULT 0,
  `TeamKills` INT UNSIGNED NOT NULL DEFAULT 0,
  `DeathCount` INT UNSIGNED NOT NULL DEFAULT 0,
  `ReviveCount` INT UNSIGNED NOT NULL DEFAULT 0,
  `CaptureCount` INT UNSIGNED NOT NULL DEFAULT 0,
  UNIQUE INDEX `idx_PlayerStats_uniquePlayer` (`PlayerUID` ASC),
  CONSTRAINT `fk_PlayerStats_PlayerInfo`
    FOREIGN KEY (`PlayerUID`)
    REFERENCES `PlayerInfo` (`UID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `PlayerInfo`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `PlayerInfo` (
  `UID` VARCHAR(32) NOT NULL,
  `CreationDate` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
  `Name` VARCHAR(256) CHARACTER SET 'utf8' NULL,
  `LastSide` VARCHAR(32) NULL,
  `BankMoney` FLOAT NOT NULL DEFAULT 0,
  `BattlEyeGUID` VARCHAR(32) NULL,
  PRIMARY KEY (`UID`))
ENGINE = InnoDB;
5

5 Answers

1
votes

I think you want to join the tables rather than select from both as you are. try:

select * from `playerinfo` i
left outer join `playerstats` s on s.`PlayerUID`=i.`UID`
limit 0,10;
1
votes

I think your problem at SQL query, you get all from two tables, that's why it dublicate:

"select * from playerstats,playerinfo limit 0,10"

try this one: "select * from playerinfo limit 0,10"

0
votes

you can select DISTINCT values from database,

$query=mysql_query("select DISTINCT name from playerstats,playerinfo limit 0,10")  or die(mysql_error());
0
votes

You seem to have different results in that scoreboard image you showed. Are they separate games played? How do you put them in your database? New game = adding a new row? If there are multiple database entries with the same nicks, then obviously your code there will output them all.

0
votes

Here is the 2 tables i am trying to fetch from

-- -----------------------------------------------------
-- Table `PlayerStats`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `PlayerStats` (
  `PlayerUID` VARCHAR(32) NOT NULL,
  `LastModified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `PlayerKills` INT UNSIGNED NOT NULL DEFAULT 0,
  `AIKills` INT UNSIGNED NOT NULL DEFAULT 0,
  `TeamKills` INT UNSIGNED NOT NULL DEFAULT 0,
  `DeathCount` INT UNSIGNED NOT NULL DEFAULT 0,
  `ReviveCount` INT UNSIGNED NOT NULL DEFAULT 0,
  `CaptureCount` INT UNSIGNED NOT NULL DEFAULT 0,
  UNIQUE INDEX `idx_PlayerStats_uniquePlayer` (`PlayerUID` ASC),
  CONSTRAINT `fk_PlayerStats_PlayerInfo`
    FOREIGN KEY (`PlayerUID`)
    REFERENCES `PlayerInfo` (`UID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `PlayerInfo`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `PlayerInfo` (
  `UID` VARCHAR(32) NOT NULL,
  `CreationDate` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
  `Name` VARCHAR(256) CHARACTER SET 'utf8' NULL,
  `LastSide` VARCHAR(32) NULL,
  `BankMoney` FLOAT NOT NULL DEFAULT 0,
  `BattlEyeGUID` VARCHAR(32) NULL,
  PRIMARY KEY (`UID`))
ENGINE = InnoDB;