0

I am making a database that when users register, they pick 3 games they would like to play. the games are stored in a separate table (gameinfo) from the user information table (personalinformation). I am querying with the first game being shown but I would like all three shown for each user. How would I implement showing all the games?

I have tried to create different variables for each game, but that has seemed to not work as I expected and broke. the games when they are stored on the personalinformation table are stored as numbers like 1 or 2. these are linked to the gameinfo table and are the primary key for each game.

Structure of database
https://i.stack.imgur.com/KtoDU.jpg

    $conn = mysqli_connect('localhost', 'root', '', 'esportclub');
    $sql = "SELECT user_ID, username, Email, Gender, firstName, lastName, gameName FROM personalinformation, gameinfo WHERE game_id = firstGame";
    $result = mysqli_query ($conn, $sql);


    if (mysqli_num_rows($result) > 0) {
      echo "<table>";

      while($row = mysqli_fetch_assoc($result)) {
        echo " <tr><td> Name: ". $row{"username"}. " </td><td> Email: ". $row{"Email"}.  " </td><td> Gender: ". $row{"Gender"}. "</td>" .
          "<td> First Name: ". $row{"firstName"}. " </td><td> First Game: ". $row{"gameName"}. "</td><td> Last Name: ". $row{"lastName"}. "</td>" . "</td></tr>" ;
      }
      echo "</table>";
    } 
    else{
      echo "0 results";
    }
    $conn->close();
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    Can we see your database structure? This seems like a ["many-to-many` relationship](https://stackoverflow.com/a/3113938/924299). I'd use a reference table to associate users and games by storing pairs of `user_ID` and `game_id`. Then [`JOIN`](https://en.wikipedia.org/wiki/Join_(SQL)) the tables together accordingly. – showdev Apr 09 '19 at 23:41
  • I have added a link to an imgur post with the screenshots of my database. I have also realised that the relationship is a many to many relationship. – Jackson North Apr 10 '19 at 00:04
  • You would need to join on FirstGame, SecondGame, and ThirdGame in order to get all 3. Another option would be to put the User_ID in the gameinfo table and join on User_ID, or do something like what @showdev said. – wavery Apr 10 '19 at 00:16

1 Answers1

1

As mentioned in my comment, I would create a table to associate users and games by storing unique pairs of user_ID and game_id values. Then I'd JOIN the tables together accordingly.

However, I see that you are storing three game values for each user in the personalinformation table, in columns named firstGame,secondGame, and thirdGame.

In that case, you can JOIN the game table to each of those columns.
So, with your existing structure:

SELECT
    p.*,
    game1.`gameName` as `firstGame_name`,
    game2.`gameName` as `secondGame_name`,
    game3.`gameName` as `thirdGame_name`
FROM `personalinformation` p
LEFT JOIN `games` as game1 ON (game1.`game_id` = p.`firstGame`)
LEFT JOIN `games` as game2 ON (game2.`game_id` = p.`secondGame`)
LEFT JOIN `games` as game3 ON (game3.`game_id` = p.`thirdGame`)
WHERE 1; // or WHERE p.`user_ID` = :user_ID;

EDIT

Since many users can own a game and a user can own many games, it sounds like a "many-to-many" relationship.

Here is my preferred method for that type of relationship. One advantage is that you don't need to limit the number of assigned games. That is, a user can own any number of games.

Create a third table to store unique user/game pairs.
It will tells you which games are assigned to which users.
Something like:

CREATE TABLE `user_game` (
  `user_id` MEDIUMINT NOT NULL ,
  `game_id` MEDIUMINT NOT NULL
);
ALTER TABLE `user_game`
  ADD UNIQUE `unique pair` (`user_id`, `game_id`);

Then join the three tables together:

SELECT
    u.*,
    g.`game_id`,
    g.`gameName`
FROM `personalinformation` u
LEFT JOIN `user_game` as ug ON ( ug.`user_id` = u.`user_ID` )
LEFT JOIN `games` as g ON ( g.`game_id` = ug.`game_id` )
WHERE 1;

You'll get back one row for every user/game relationship.
If one user has three games, that user will have three rows in the result, each row including one gameName.

For example:

Name   Game
----   -----------------
Jane   League of Legends
Jane   Minecraft
Fred   Dota 2
Alex   Minecraft
Alex   War Dragons
Alex   Fortnite

More complex display might require some processing:

<?php

$users = array();

while($row= mysqli_fetch_object($result)) {

    $uid = $row->user_ID;

    // if this user isn't in the array...
    if (!array_key_exists($uid,$users)) {

        // ... create a user entry ...
        $user = new stdClass();
        $user->firstname = $row->firstName;

        // ... and add it to the user array.
        $users[$uid] = $user;

    }

    // if this row has a valid game ...
    if (!empty($row->game_id)) {

        // ... create a game entry ...
        $game = new stdClass();
        $game->id = $row->game_id;
        $game->name = $row->gameName;

        //.. and add the game to the user's entry
        $users[$uid]->games[$game->id]=$game;

    }

}

For a structure like this:

Array
(
    [1] => stdClass Object
        (
            [firstname] => Jane
            [games] => Array
                (
                    [1] => stdClass Object
                        (
                            [id] => 1
                            [name] => Leage of Legends
                        )

                    [2] => stdClass Object
                        (
                            [id] => 2
                            [name] => Minecraft
                        )

                )

        )

    [2] => stdClass Object
        (
            [firstname] => Fred
            [games] => Array
                (
                    [3] => stdClass Object
                        (
                            [id] => 3
                            [name] => Dota 2
                        )

                )

        )

    [3] => stdClass Object
        (
            [firstname] => Alex
            [games] => Array
                (
                    [2] => stdClass Object
                        (
                            [id] => 2
                            [name] => Minecraft
                        )

                    [4] => stdClass Object
                        (
                            [id] => 4
                            [name] => War Dragons
                        )

                    [5] => stdClass Object
                        (
                            [id] => 5
                            [name] => Fortnite
                        )

                )

        )

)
showdev
  • 28,454
  • 37
  • 55
  • 73