0

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 Answers 5

1

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;
Sign up to request clarification or add additional context in comments.

Comments

1

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"

Comments

0

you can select DISTINCT values from database,

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

4 Comments

heres the 2 tables im trying to fetch data from
you should add relationships between the table and than use joins to get the data.
If I fetch playerUID it will show the player uid wich is not the playername its numbers like (8303979326389)
will add all other fields in select DISTINCT playerUID,player Kills,etc and use joins
0

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.

1 Comment

2 colums from 1 table and the others for the other table since the way the game database is made is separate tables
0

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;

1 Comment

Please put it in the original post (use the "edit" option) instead of submitting it as an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.