2 tables, 3 columns need 1 value

Viewed 74

I am trying to get 5 bits of data, 5 of them come from tbl_playerstats these columns are

StatsID 

Score

Kills

Deaths

Rank

What im struggling with it that StatsID is linked to another table called tbl_playerdata In this table the PlayerID is the same as StatsID their values are numeric, also in this tbl_playerdata is SoldierName which is what im am trying to put in place of StatsID , end goal being that only these show:

SoldierName

Score

Kills

Deaths

Rank

My code so far looks like this ( minus database connection details

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT StatsID, Score, Kills, Deaths, Rounds FROM tbl_playerstats Limit 10";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<br> Soldier: ". $row["StatsID"]. "<p> Score: ". $row["Score"]. "<p> Kills: ". $row["Kills"]. " <p>Deaths: " . $row["Deaths"] . $row["Rounds"] . "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();

I have googled and it looks like using a JOIN is the way to go, the examples that I have found haven't worked for me, this could be down to not really knowing what this is called I'm trying to do.

3 Answers
Related